📜  Java TimeZone类

📅  最后修改于: 2020-10-01 06:45:33             🧑  作者: Mango

Java TimeZone类

Java TimeZone类表示时区偏移量,还可以计算夏令时。它继承了Object类。

Java TimeZone类声明

让我们看看java.util.TimeZone类的声明。

public abstract class TimeZone extends Object  implements Serializable, Cloneable

Java TimeZone的方法

Method Description
static String[] getAvailableIDs() It is used to get all the available IDs supported.
static TimeZone getDefault() It is used to get the default TimeZone for this host.
String getDisplayName() It is used to return a name of this time zone suitable for presentation to the user in the default locale.
String getID() It is used to get the ID of this time zone
int getOffset(long date) It is used to return the offset of this time zone from UTC at the specified date.
void setID(String ID) It is used to set the time zone ID

Java TimeZone类示例:getAvailableIDs()

import java.util.*;
public class TimeZoneExample1 {
   public static void main( String args[] ){       
   String[] id = TimeZone.getAvailableIDs();      
   System.out.println("In TimeZone class available Ids are: ");
   for (int i=0; i

输出:

In TimeZone class available Ids are: 
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau and so on ....

Java TimeZone类示例:getOffset()

import java.util.*;
public class TimeZoneExample2 {
   public static void main( String args[] ){    
   TimeZone zone = TimeZone.getTimeZone("Asia/Kolkata"); 
   System.out.println("The Offset value of TimeZone: " + 
   zone.getOffset(Calendar.ZONE_OFFSET));
   }    
}

输出:

The Offset value of TimeZone: 19800000

Java TimeZone类示例:getID()

import java.util.*;
public class TimeZoneExample3 {
   public static void main( String args[] ){
   TimeZone timezone = TimeZone.getTimeZone("Asia/Kolkata");     
   System.out.println("Value of ID is: " + timezone.getID());
   }    
}

输出:

Value of ID is: Asia/Kolkata

Java TimeZone类示例:getDisplayName()

import java.util.*;
public class TimeZoneExample4 {
   public static void main( String args[] ){
   TimeZone zone = TimeZone.getDefault();
   String name = zone.getDisplayName();       
   System.out.println("Display name for default time zone: "+ name);
   }    
}

输出:

Display name for default time zone: India Standard Time