TimeZone类用于表示时区偏移,并计算夏令时。 什么是时区和时间偏移? “时区”用于描述世界不同地区的当前时间。它指的是世界上按经度划分的24个区域中的一个特定区域。在这些地区中的每一个,都保持着标准的时间版本。
null
- 根据它们与协调世界时或UTC的关系计算不同的时区。
- 时间偏移是从世界时(Universal time)时间中减去或加上的时间量,以获得当前的民用时间,无论是标准时间还是夏令时(DST)。
- 我们根据经度将整个地球从东到西划分为24个不同的区域,每个区域宽15度。因此,地球上有24个不同的时区。每个时区宽15度,每个时区之间相差一小时。
- 根据从格林威治子午线向东或向西的距离,必须为每15度的经度间隔加上或减去适当的时间。
例如: 要找到特定位置的时区(以小时为单位),可以将经度除以15。例如,105°E等于105/15,等于7。这意味着时区比UTC或GMT时间提前7小时,也可以标记为UTC+7。其中7是该位置的时间偏移。
Java中的时区类
类别声明
public abstract class TimeZone extends Object implements Serializable, Cloneable
时区类的方法:
- getAvailableIDs() –使用此方法,您可以获得所有可用的时区ID。
Syntax : public static String[] getAvailableIDs()
- getAvailableIDs(int rawOffset) –使用此方法可以获得一个ID数组,其中该ID的时区具有指定的GMT偏移量(以毫秒为单位)。
Syntax : public static String[] getAvailableIDs(int rawOffset)Parameters: rawOffset - the given time zone GMT offset in milliseconds.
JAVA
// Java program for Demonstration of // getAvailableIDs() and // getAvailableIDs(int rawOffset ) methods import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // get all the timezones ids defined by TimeZone class String[] availableTimezones = TimeZone.getAvailableIDs(); // Print Total no of TimeZones System.out.println( "Total No of Time Zone Available" ); System.out.println(availableTimezones.length); // get all the timezones whose offset is // 7200000 milliseconds means 2 hour String[] timezones = TimeZone.getAvailableIDs( 7200000 ); // Print Total no of TimeZones System.out.println( "No of Time Zone having time offset 2 hour" ); System.out.println(timezones.length); // print all timezones names System.out.println( "Timezone names having time offset 2 hour" ); for ( int i = 0 ; i < timezones.length; i++) System.out.println(timezones[i]); } } |
Output: Total No of Time Zone Available628No of Time Zone having a time offset 2 hour43Timezone names having a time offset 2 hourARTAfrica/BlantyreAfrica/BujumburaAfrica/Cairo..................
- getDefault() –使用此方法,您可以获得程序运行所在地的时区。
Syntax : public static TimeZone getDefault()
- getDisplayName() –方法返回初始化时区的长标准时间名称。
Syntax : public final String getDisplayName()
JAVA
// Java program for Demonstration of // getDefault() and getDisplayName() methods import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // Get your Local Time Zone Where this Program is Running. TimeZone timezone = TimeZone.getDefault(); // Get the Name of Time Zone String LocalTimeZoneDisplayName = timezone.getDisplayName(); // Print the Name of Time Zone System.out.println(LocalTimeZoneDisplayName); } } |
Output: Coordinated Universal Time
- getTimeZone(字符串ID) –此方法用于获取给定ID的时区。
Syntax :public static TimeZone getTimeZone(String ID)Parameters: ID - the ID for a TimeZone.
- getDSTSavings() –方法返回要添加到本地标准时间以获取本地挂钟时间的时间量。
Syntax : public int getDSTSavings()
- getID() –此方法用于获取此时区的ID。
Syntax : public String getID()
JAVA
// Java program for Demonstration of // getTimeZone(String ID), // getDSTSavings() and getID() methods import java.sql.Time; import java.util.TimeZone; public class TimeZoneDemo { public static void main(String[] args) { // creating Timezone object whose id is Europe/Berlin TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); // printing the Display Name of this timezone object System.out.println( "Display Name" ); System.out.println(timezone.getDisplayName()); // getting DST in milliseconds int timeInMilliseconds = timezone.getDSTSavings(); System.out.println( "DST of Europe/Berlin is" ); System.out.println(timezone.getDSTSavings()); // get Id of your Default Time Zone TimeZone defaultTimezone = TimeZone.getDefault(); System.out.println( "The id of default Time zone is" ); System.out.println(timezone.getID()); } } |
Output: Display NameCentral European TimeDST of Europe/Berlin is3600000The id of default Time zone isEurope/Berlin
- getOffset(长日期) –该方法用于返回该时区在方法中传递的日期相对于UTC的偏移量。
Syntax : the method is used to return the offset of this time zone from UTC at the passed date in method.Parameters: date - the date represented in milliseconds since January 1, 1970 00:00:00 GMT
- inDaylightTime(日期) –如果给定日期在该时区的夏令时,则此方法返回true,否则返回false。
Syntax :Syntax : public abstract boolean inDaylightTime(Date date)Parameters:date - the given Date.
- observesDaylightTime() –如果此时区当前处于夏令时,或者如果在未来任何时间发生从标准时间到夏令时的转换,则此方法返回true。
Syntax :public boolean observesDaylightTime()
JAVA
// Java program for // Demonstration of getOffset(long date), // inDaylightTime(Date date) and // observesDaylightTime() methods import java.sql.Time; import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // creating Timezone object whose id is Europe/Berlin TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); // printing offset value System.out.println( "Offset value of Europe/Berlin:" ); System.out.println(timezone.getOffset(Calendar.ZONE_OFFSET)); // create Date Object Date date = new Date( 2017 , 04 , 16 ); // checking the date is in day light time of that Time Zone or not System.out.println( "Date 16/04/2017 is in Day Light Time of" ); System.out.println( "Timezone: timezone.getDisplayName()" ); System.out.println(timezone.inDaylightTime(date)); // check this Time Zone observes Day Light Time or Not System.out.println( "TimeZone name " + timezone.getDisplayName()); System.out.println( "Observes Day Light Time" ); System.out.println(timezone.observesDaylightTime()); } } |
Output:Offset value of Europe/Berlin:3600000Date 16/04/2017 is in Day Light Time ofTimezone: timezone.getDisplayName()trueTimeZone name Central European TimeObserves Day Light Timetrue
- 设置默认值(时区) –用于设置getDefault方法返回的时区。
Syntax : public static void setDefault(TimeZone zone)Parameters: zone - the new default time zone
- setID(字符串ID) –用于设置时区ID。
Syntax :public void setID(String ID)Parameters: ID - the new time zone ID.
- 克隆() –此方法用于创建此时区的副本
Syntax : public Object clone()
JAVA
// Java program for Demonstration of // setDefault(TimeZone zone), // setID(String ID) and clone() methods import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // My previous Default Time Zone is TimeZone DefaultTimeZone = TimeZone.getDefault(); System.out.println( "Current Default TimeZone:" ); System.out.println(DefaultTimeZone.getDisplayName()); // Setting Europe/Berlin as your Default Time Zone TimeZone timezone = TimeZone.getTimeZone( "Europe/Berlin" ); timezone.setDefault(timezone); TimeZone NewDefaultTimeZone = TimeZone.getDefault(); System.out.println( "New Default TimeZone:" ); System.out.println(NewDefaultTimeZone.getDisplayName()); // change Id Europe/Berlin to Eur/Ber timezone.setID( "Eur/Ber" ); System.out.println( "New Id of Europe/Berlin is" ); System.out.println(timezone.getID()); // create copy of a time zone System.out.println( "Original TimeZone ID:" ); System.out.println(timezone.getID()); TimeZone clonedTimezone = (TimeZone)timezone.clone(); System.out.println( "Cloned TimeZone ID:" ); System.out.println(clonedTimezone.getID()); } } |
Output:Current Default TimeZone:India Standard TimeNew Default TimeZone:Central European TimeNew Id of Europe/Berlin isEur/BerOriginal TimeZone ID:Eur/BerCloned TimeZone ID:Eur/Ber
例子: 打印程序运行的任何给定输入时区的日期和时间。
JAVA
// Java program to illustrate // java.util.timezone class import java.text.*; import java.util.*; public class TimeZoneDemo { public static void main(String[] args) { // Get your Local Time Zone Where this Program is Running. TimeZone timezone = TimeZone.getDefault(); // Get the Name of Time Zone String LocalTimeZoneName = timezone.getDisplayName(); // Initialize your Date Object and Date Format to represent your Date Date date = new Date(); DateFormat dformat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); // set your local time Zone to your Date Format time Zone dformat.setTimeZone(timezone); // Print Date and Time for your Time Zone System.out.println( "Date and time of your Local Time Zone:" ); System.out.println(LocalTimeZoneName + ", " + dformat.format(date)); } } |
Output: Date and time of your Local Time Zone:Coordinated Universal Time, 2018-04-17 07:36:19
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END