时区转换 [英] Timezone conversion

查看:25
本文介绍了时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的项目中从一个时区转换为另一个时区.

I need to convert from one timezone to another timezone in my project.

我可以从我当前的时区转换到另一个时区,但不能从不同的时区转换到另一个时区.

I am able to convert from my current timezone to another but not from a different timezone to another.

例如,我在印度,我可以使用 Date d=new Date(); 从印度转换为美国,并将其分配给日历对象并设置时区.

For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.

但是,我无法从不同时区到另一个时区执行此操作.例如,我在印度,但无法将时区从美国转换为英国.

However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.

推荐答案

tl;dr

ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" ))              // Current moment in a particular time zone.
             .withZoneSameInstant( ZoneId.of( "Asia/Kolkata" ))  // Same moment adjusted into another time zone. 

详情

java.util.Date 类没有分配时区,但它的 toString 实现混淆地应用了 JVM 的当前默认时区.

Details

The java.util.Date class has no time zone assigned, yet it's toString implementation confusingly applies the JVM's current default time zone.

这是避免与 Java 捆绑在一起的臭名昭著的 java.util.Date、.Calendar 和 SimpleDateFormat 类的众多原因之一.避开它们.而是使用:

This is one of many reasons to avoid the notoriously troublesome java.util.Date, .Calendar, and SimpleDateFormat classes bundled with Java. Avoid them. Instead use either:

  • The java.time package built into Java 8 and inspired by Joda-Time.
  • Joda-Time

Java 8 及更高版本具有 java.time 包 内置.这个包的灵感来自 Joda-Time.虽然它们有一些相似之处和类名,但它们是不同的;每个都有另一个缺乏的功能.一个显着的区别是 java.time 避免使用构造函数,而是使用静态实例化方法.这两个框架都由同一个人 Stephen Colbourne 领导.

Java 8 and later has the java.time package built-in. This package was inspired by Joda-Time. While they share some similarities and class names, they are different; each has features the other lacks. One notable difference is that java.time avoids constructors, instead uses static instantiation methods. Both frameworks are led by the same man, Stephen Colbourne.

大部分 java.time 功能已向后移植到 Java 6 &7 在 ThreeTen-Backport 项目中.在 ThreeTenABP 项目中进一步适应 Android.

Much of the java.time functionality has been back-ported to Java 6 & 7 in the ThreeTen-Backport project. Further adapted to Android in the ThreeTenABP project.

在这个问题的情况下,它们以相同的方式工作.指定一个时区,并调用一个 now 方法获取当前时刻,然后基于旧的不可变实例创建一个新实例以调整时区.

In the case of this Question, they work in the same fashion. Specify a time zone, and call a now method to get current moment, then create a new instance based on the old immutable instance to adjust for time zone.

注意两个不同的时区类别.一个是命名时区,包括夏令时和其他此类异常的所有规则以及与 UTC 的偏移量,而另一个只是偏移量.

Note the two different time zone classes. One is a named time zone including all the rules for Daylight Saving Time and other such anomalies plus an offset from UTC while the other is only the offset.

ZoneId zoneMontréal = ZoneId.of("America/Montreal"); 
ZonedDateTime nowMontréal = ZonedDateTime.now ( zoneMontréal );

ZoneId zoneTokyo = ZoneId.of("Asia/Tokyo"); 
ZonedDateTime nowTokyo = nowMontréal.withZoneSameInstant( zoneTokyo );

ZonedDateTime nowUtc = nowMontréal.withZoneSameInstant( ZoneOffset.UTC );

乔达时间

Joda-Time 2.3 中的一些示例代码如下.搜索 StackOveflow 以获取更多示例和讨论.

Joda-Time

Some example code in Joda-Time 2.3 follows. Search StackOveflow for many more examples and much discussion.

DateTimeZone timeZoneLondon = DateTimeZone.forID( "Europe/London" );
DateTimeZone timeZoneKolkata = DateTimeZone.forID( "Asia/Kolkata" );
DateTimeZone timeZoneNewYork = DateTimeZone.forID( "America/New_York" );

DateTime nowLondon = DateTime.now( timeZoneLondon ); // Assign a time zone rather than rely on implicit default time zone.
DateTime nowKolkata = nowLondon.withZone( timeZoneKolkata );
DateTime nowNewYork = nowLondon.withZone( timeZoneNewYork );
DateTime nowUtc = nowLondon.withZone( DateTimeZone.UTC );  // Built-in constant for UTC.

在宇宙的时间轴上,我们有四个表示同一时刻.

We have four representations of the same moment in the timeline of the Universe.

实际上 java.util.Date确实其源代码.但是出于大多数实际目的,该类忽略了该时区.因此,作为速记,人们常说 j.u.Date 没有分配时区.令人困惑?是的.避免 j.u.Date 的混乱,使用 Joda-Time 和/或 java.time.

Actually the java.util.Date class does have a time zone buried within its source code. But the class ignores that time zone for most practical purposes. So, as shorthand, it’s often said that j.u.Date has no time zone assigned. Confusing? Yes. Avoid the mess that is j.u.Date and go with Joda-Time and/or java.time.

这篇关于时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆