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 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 date time API 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开始,对于日期时间API ,时区由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 = 1515136660357
Hour at America = 23
Time at Asia in millis = 1515136660357
Hour 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: Oracle Documentation

参考: Oracle文档

翻译自: https://www.journaldev.com/17942/java-timezone

java时区

java时区_Java时区相关推荐

  1. java date类 时区_Java时区转换及Date类实现原理解析

    这篇文章主要介绍了Java时区转换及Date类实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一.时区的说明 地球表面按经线从东到西,被 ...

  2. java 日本时区_java时区时间ZoneOffset, ZoneId,OffsetTime,OffsetDateTi

    前言 ZoneOffset,LocalDateTime,LocalTime, YearMonth, Year, MonthDay,它们代表与上下文相结合的本地日期/时间.这些类主要用于不需要在上下文中 ...

  3. java 转时区_java – 时区转换

    一些例子 import java.util.Calendar; import java.util.GregorianCalendar; import java.util.TimeZone; publi ...

  4. 东八区转为0时区_Java时区转换及时间格式

    本文介绍Java API 中 Date, Calendar, TimeZone和DateFormat的使用,以及不同时区时间相互转化的方法和原理. 问题描述: 向处于不同时区的服务器发请求时需要考虑时 ...

  5. java欧洲_java欧洲/明斯克时区问题

    我写了以下程序: import sun.security.action.GetPropertyAction; import java.security.AccessController; import ...

  6. 时区 java 巴黎_java中的时区陷阱 - iasuna

    今天碰到一个很奇怪的问题,就是在调用SimpleDateFormat类的parse方法,得到了意外的结果. public static void main(String[] args) throws ...

  7. java 夏令时区_Java中的夏令时问题

    因为在用C#做项目的时候被夏令时坑过一回,所以这次将在java中的时区转换信息做一下记录,很简单 SimpleDateFormat inputFormat = new SimpleDateFormat ...

  8. 【java】带时区的时间格式化

    前言 jdk1.8 当前时区 带时区格式化时间 为SimpleDateFormat对象指定时区后,再执行格式化. Calendar cal = Calendar.getInstance(); Simp ...

  9. 时区 java 巴黎,关于时区:Java没有有关所有IANA时区的信息

    我正在尝试将来自前端的值映射到ZoneId类,如下所示: Optional.ofNullable(timeZone).map(ZoneId::of).orElse(null) 在大多数时区,它工作正常 ...

最新文章

  1. 为什么很多优秀的人,把闹钟定在早晨5:57?
  2. python中list作为函数参数_在python中list作函数形参,防止被实参修改的实现方法
  3. 计算机网络-基本概念(2)【网络层】-网际控制报文协议ICMP
  4. 牛客多校6 - Harmony Pairs(数位dp)
  5. yii3正式版什么时候发布_华为mate50pro什么时候发布
  6. java面试知识记录
  7. centos 7 之nginx
  8. 由儿时的游戏打保猜最广想到的
  9. 阶乘浅析poj1150 3406 zoj1222 2358
  10. mysql5.8安装
  11. 文献检索与论文写作——学习笔记
  12. 郑州中小幼计算机初中级,郑州市中小幼计算机等级考试大纲.doc
  13. C/C++新手入门教程:傻瓜都会的VS2013使用教程,成为程序员的第一步
  14. 微信小程序 - - 授权登录退出和缓存
  15. 《机电传动控制》——直流电机调速仿真作业
  16. OPENSSL EVP_AES部分翻译
  17. BODIPY493/503-alkyne/amine/azide/carboxylic acid/DBCO/hydrazide/maleimide/NHS /tetrazine氟化硼二吡咯衍生物
  18. Html上传文件php处理上传文件
  19. 自由落体java编程_java模拟自由落体运动源代码
  20. python中的screen blit_python pygame blit。获取要显示的图像

热门文章

  1. C#中自己动手创建一个Web Server(非Socket实现)
  2. 浏览器控制台console
  3. Java Socket文件上传
  4. android (java) 网络发送get/post请求参数设置
  5. [转]导出数据到Excel的几种方法
  6. 多线程并发思考--文件加锁
  7. ASP.NET控件在2.0和1.1下运行的一个微小的差别
  8. Vue.js 学习笔记 九 v-if和v-show
  9. 码农人生-如何快速使用从未学过的语言实现项目开发-第002期博文
  10. 【mysql优化 2】索引条件下推优化