本文共 5329 字,大约阅读时间需要 17 分钟。
java时区
Java TimeZone class represents a time zone offset, and also figures out daylight savings. Time zone is a region of the globe that observes a uniform standard time for all the purposes. Time zone is important for programs as well as it gives the user a feel of the application being local to the user.
Java TimeZone类表示时区偏移量,还可以计算夏令时。 时区是地球上的一个区域,出于所有目的,该区域均遵循统一的标准时间。 时区对于程序很重要,它使用户感觉应用程序在用户本地。
java.util
package and should be used along with the Calendar
class. Java TimeZone类用于在程序中实现和操纵各种TimeZone。 此类是java.util
包的一部分,应与Calendar
类一起使用。 Starting from Java 8, for the time zones are represented by the java.time.ZoneId
class. This is needed only if you are using the Java 8 date time classes like the ZonedDateTimeclass
. If you use a Calendar class from the Java 7 and earlier date time API you can still use the java.util.TimeZone
class.
从Java 8开始,对于 ,时区由java.time.ZoneId
类表示。 仅在使用Java 8日期时间类(例如ZonedDateTimeclass
。 如果您使用Java 7和更早的日期时间API中的Calendar类,则仍可以使用java.util.TimeZone
类。
There are two ways of creating a TimeZone object.
有两种创建TimeZone对象的方法。
TimeZone tz = TimeZone.getDefault();
If the above mentioned program is running in India, the default time zone that is IST will be provided as the TimeZone object.
使用getDefault()方法 :TimeZone类包含一个getDefault()方法,该方法根据应用程序或程序在其中运行的时区提供TimeZone对象。如果上述程序在印度运行,则默认时区IST将作为TimeZone对象提供。
TimeZone tz = TimeZone.getTimeZone(“America/Chicago”)使用getTimeZone()方法 :TimeZone类包含getTimeZone()方法,其中该方法的输入参数是时区ID。
We discussed in the introduction section that TimeZone should be used along with Calendar. Let’s try to understand how it should be done.
我们在简介部分讨论了TimeZone应该与Calendar一起使用。 让我们尝试了解它应该如何完成。
For using TimeZone with Calendar we need an instance of the Calendar class. We will look at an example of how to get time zone from Calendar.
为了将TimeZone与Calendar一起使用,我们需要Calendar类的实例。 我们将看一个如何从日历获取时区的示例。
Calendar calendar = new GregorianCalendar();TimeZone timeZone = calendar.getTimeZone();
Now if we want to set the time zone for Calendar instance we can perform that task as follows.
现在,如果要为Calendar实例设置时区,我们可以按以下步骤执行该任务。
calendar.setTimeZone(timeZone);
getDisplayName()
: A standard time name of the TimeZone which suitable for presentation to the user in the default locale.TimeZone tz = TimeZone.getDefault();System.out.println(tz.getDisplayName()) //India Standard Time
getDisplayName()
:TimeZone的标准时间名称,适合在默认语言环境中呈现给用户。 getID()
: Returns the ID of the time zone.TimeZone tz = TimeZone.getDefault();System.out.println(tz.getID()); //Asia/Calcutta
getID()
:返回时区的ID。 getOffset(long date)
: Returns the offset of this time zone from UTC at the specified date.TimeZone tz = TimeZone.getDefault();long sec = System.currentTimeMillis();System.out.println(tz.getOffset(sec)); //19800000
getOffset(long date)
:返回该时区在指定日期与UTC的偏移量。 We can use time zone conversion as well for converting the time zone based on the ID that is provided, this use case is needed when the application is running in two specific time zones.
我们也可以使用时区转换来根据提供的ID转换时区,当应用程序在两个特定时区中运行时,需要这种用例。
Here is an example for converting a date from Calendar to different time zones.
这是将日期从日历转换为不同时区的示例。
package com.journaldev.java;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;public class TimeZoneExample { public static void main(String[] args) { TimeZone tzLA = TimeZone.getTimeZone("America/Los_Angeles"); TimeZone tzIN = TimeZone.getTimeZone("Asia/Calcutta"); Calendar calendar = new GregorianCalendar(); calendar.setTimeZone(tzLA); long timeLA = calendar.getTimeInMillis(); System.out.println("Time at America in milliseconds = " +timeLA); System.out.println("Hour at America = " +calendar.get(Calendar.HOUR_OF_DAY)); calendar.setTimeZone(tzIN); long timeIN = calendar.getTimeInMillis(); System.out.println("Time at Asia in millis = " + timeIN); System.out.println("Hour at Asia = " + calendar.get(Calendar.HOUR_OF_DAY)); }}
Output produced by above example:
上面的示例产生的输出:
Time at America in milliseconds = 1515136660357Hour at America = 23Time at Asia in millis = 1515136660357Hour at Asia = 12
In the example above, the time denoted by milliseconds is same for America and Asia but there is a difference in the hour field representing the change in the time zones.
在上面的示例中,以毫秒表示的时间在美洲和亚洲是相同的,但是表示时区变化的小时字段存在差异。
We can get the list of ID available for using with TimeZone by using getAvailableIDs() and iterating through the result of the method.
我们可以使用getAvailableIDs()并遍历方法的结果来获取可用于TimeZone的ID列表。
String[] tzIDs = TimeZone.getAvailableIDs();for(String id : tzIDs) System.out.println(id);
That’s all for Java TimeZone class.
这就是Java TimeZone类的全部内容。
Reference:
参考:
翻译自:
java时区
转载地址:http://cumzd.baihongyu.com/