博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java时区_Java时区
阅读量:2532 次
发布时间:2019-05-11

本文共 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时区 (Java TimeZone)

Java TimeZone
Java TimeZone class is used for implementation and manipulation of various TimeZone across the program. This class is part of the
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类。

创建Java TimeZone实例 (Creating Java TimeZone instance)

There are two ways of creating a TimeZone object.

有两种创建TimeZone对象的方法。

  1. Using getDefault() method: TimeZone class contains a getDefault() method which provides a TimeZone object based on the time zone in which the application or the program is running.
    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对象提供。

  2. Using getTimeZone() method: TimeZone class contains getTimeZone() method, where the input parameter for the method is a time zone ID.
    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一起使用。 让我们尝试了解它应该如何完成。

在日历中使用TimeZone (Using TimeZone with 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);

Java TimeZone方法 (Java TimeZone Methods)

  • 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转换时区,当应用程序在两个特定时区中运行时,需要这种用例。

Java TimeZone转换 (Java TimeZone Conversion)

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.

在上面的示例中,以毫秒表示的时间在美洲和亚洲是相同的,但是表示时区变化的小时字段存在差异。

Java TimeZone ID (Java TimeZone ID)

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/

你可能感兴趣的文章
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-1.快速搭建SpringBoot项目,采用Eclipse...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-4.在线教育后台数据库设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-3.热部署在Eclipse和IDE里面的使用...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_1-3.在线教育站点需求分析和架构设计...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-4.后端项目分层分包及资源文件处理...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-2.快速搭建SpringBoot项目,采用IDEA...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_3-5.PageHelper分页插件使用
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-6.微信扫码登录回调本地域名映射工具Ngrock...
查看>>
小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_5-8.用户模块开发之保存微信用户信息...
查看>>
Linux下Nginx安装
查看>>
LVM扩容之xfs文件系统
查看>>
Hbase记录-client访问zookeeper大量断开以及参数调优分析(转载)
查看>>
代码片段收集
查看>>
vue-cli3创建项目时报错
查看>>
输入1-53周,输出1-53周的开始时间和结束时间
查看>>
实验二
查看>>
shell——按指定列排序
查看>>
crash 收集
查看>>
507 LOJ 「LibreOJ NOI Round #1」接竹竿
查看>>
UI基础--烟花动画
查看>>