如何在Java中将时区字符串转换为TimeZone对象? [英] How can i convert time zone string to the TimeZone Object in java?
问题描述
我有几个UTC格式的时区字符串,例如"UTC + 08:00","UTC-05:00"
,问题是如何转换这些utc格式Java中 java.util.TimeZone
的字符串?
I have several time zone strings in UTC format, such as "UTC+08:00", "UTC-05:00"
, the question is how can i convert these utc format strings to the java.util.TimeZone
in Java?
我曾尝试通过 ZoneId
进行如下转换,但没有成功:
I have tried to convert by ZoneId
as follows, but it did not work:
ZoneId zoneId = ZoneId.of("UTC+08:00");
TimeZone timeZone = TimeZone.getTimeZone(zoneId);
我知道 TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
可以工作,但是我不知道"UTC + 08:00"
和亚洲/上海"
I know TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai");
would work, but I do not know the mapping between "UTC+08:00"
and "Asia/Shanghai"
推荐答案
tl; dr
- 不要不使用
TimeZone
类(现在是旧版). - 改为使用
ZoneOffset
和ZoneId
. - Do not use
TimeZone
class (now legacy). - Use
ZoneOffset
andZoneId
instead.
tl;dr
示例:
ZoneOffset.of( "+08:00" )
使用 java.time.ZoneId
,而不是 TimeZone
与最早的Java版本捆绑在一起的麻烦的旧日期时间类现在已被遗留,由java.time类取代.这些旧的旧类中有 TimeZone
,现在已由 ZoneId
和 ZoneOffset
.
Use java.time.ZoneId
, not TimeZone
The troublesome old date-time classes bundled with the earliest versions of Java are now legacy, supplanted by the java.time classes. Among these old legacy classes is TimeZone
, now supplanted by ZoneId
and ZoneOffset
.
UTC偏移量是指提前数小时和数分钟的调整,或后面的 UTC .这由 ZoneOffset
类表示.
An offset-from-UTC is a number of hours and minutes adjustment ahead of, or behind, UTC. This is represented by the ZoneOffset
class.
时区是偏移量的集合,即偏移量的变化历史由特定区域在确定其挂钟时间时使用.这由 ZoneId
类表示.
A time zone is a collection of offsets, the history of changes in the offset used by a particular region in determining their wall-clock time. This is represented by the ZoneId
class.
使用时区始终比偏移量更可取,因为一个时区具有偏移量以及更多的信息.但是您的示例仅是补偿.因此,在删除字符 UTC
后,使用 ZoneOffset
解析字符串.
Using a time zone is always preferable to an offset as a zone has the offset plus so much more information. But your examples are only mere offsets. So use the ZoneOffset
to parse the strings after deleting the characters UTC
.
String input = "UTC+08:00".replace( "UTC" , "" ) ;
ZoneOffset offset = ZoneOffset.of( input ) ;
不要猜时区
您不能假设特定的偏移量意味着特定的时区.许多区域可能在过去,现在或将来使用了特定的偏移量.因此,您不应该猜测区域.
Do not guess the time zone
You cannot assume that a particular offset implies a particular time zone. Many zones may have used a particular offset in the past, present, or future. So you should not guess the zone.
以例如 +08:00
的偏移量为例.目前,几个不同的时区(包括亚洲/上海
,亚洲/澳门
和 Australia/Perth
.
Take, for example, the offset of +08:00
. That offset is currently used by several different time zones including Asia/Shangai
, Asia/Macao
, and Australia/Perth
.
If you are certain a particular zone was intended for a date-time value, apply it to get a ZonedDateTime
. But do not guess.
Instant
类表示UTC时间轴上的时刻,其分辨率为纳秒.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = Instant.now() ;
ZoneId z = ZoneId.of( "Asia/Shanghai" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
如果您不确定特定的预期时区并且只有一个偏移量,请使用该偏移量获取
If you do not know for certain the intended time zone and have only an offset, use the offset to get an OffsetDateTime
.
Instant instant = Instant.now() ;
ZoneOffset offset = ZoneOffset.of( "+08:00" ) ;
OffsetDateTime odt = instant.atOffset( offset ) ;
转换
最好避免使用旧的旧类 TimeZone
.但是,如果必须使用该类来处理尚未为java.time类更新的旧代码,则可以转换为 ZoneId
.使用添加到旧类中的新转换方法.
Convert
Best to avoid the old legacy class TimeZone
. But if you must use that class to work with old code not yet updated for the java.time classes, you can convert to/from a ZoneId
. Use the new conversion methods added to the old classes.
TimeZone myLegacyTimeZone = TimeZone.getTimeZone( myModernZoneId );
…和…
ZoneId z = myLegacyTimeZone.toZoneId() ;
请注意, ZoneOffset
是 ZoneId
的子类.通常,我们忽略该继承关系.如果只有偏移量,例如 +08:00
,请使用 ZoneOffset
.如果您有一个完整的时区,例如 Asia/Shanghai
,请使用 ZoneId
.此规则的一个例外是这种与 TimeZone
之间的转换,其中仅超类 ZoneId
被识别.
Note that ZoneOffset
is a subclass of ZoneId
. Normally, we ignore that inheritance relationship. If you have only a mere offset such as +08:00
, use ZoneOffset
. If you have a full time zone such as Asia/Shanghai
, use ZoneId
. One exception to this rule is for this conversion to/from TimeZone
where only the superclass ZoneId
is recognized.
这篇关于如何在Java中将时区字符串转换为TimeZone对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!