从带有偏移值的日期获取时区信息 [英] Get timeZone information from date with date with offset value

查看:46
本文介绍了从带有偏移值的日期获取时区信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下偏移量 -07 的日期,属于 美国/太平洋" 时区.

we have the below date having offset -07, which belong to "US/Pacific" time zone.

String startTime = "2020-06-12T09:30:00.000-07:00";

我想从上面的日期对象中检索时区信息(美国/太平洋").

I want to retrieve timezone info ("US/Pacific") from above date object.

推荐答案

您可以获得在特定时刻具有 -7h 偏移量的区域列表,但它不会是唯一的.例如,您可以使用以下代码:

You can get a list of zones that have a -7h offset at that particular moment in time, but it will not be unique. For example you could use this code:

String bookingStartTime = "2020-06-12T09:30:00.000-07:00";
OffsetDateTime odt = OffsetDateTime.parse(bookingStartTime);
Set<String> allZones = new HashSet<>();
for (String z : ZoneId.getAvailableZoneIds()) {
  ZoneId id = ZoneId.of(z);
  ZonedDateTime zdt = odt.atZoneSameInstant(id);
  if (zdt.getOffset().equals(odt.getOffset())) allZones.add(z);
}

for (String z : allZones) System.out.println(z);

输出为:

美国/太平洋-新
美国/蒂华纳
SystemV/PST8PDT
美国/亚利桑那
美国/圣诞老人_伊莎贝尔
加拿大/育空地区
加拿大/太平洋地区
美国/克雷斯顿
美国/凤凰城
美国/道森_克里克
美国/洛杉矶
美国/怀特霍斯
美国/恩塞纳达
美国/道森
PST8PDT
美洲/埃莫西约
美国/温哥华
SystemV/MST7
等/GMT+7
美国/Fort_Nelson
美国/太平洋地区
墨西哥/北下

US/Pacific-New
America/Tijuana
SystemV/PST8PDT
US/Arizona
America/Santa_Isabel
Canada/Yukon
Canada/Pacific
America/Creston
America/Phoenix
America/Dawson_Creek
America/Los_Angeles
America/Whitehorse
America/Ensenada
America/Dawson
PST8PDT
America/Hermosillo
America/Vancouver
SystemV/MST7
Etc/GMT+7
America/Fort_Nelson
US/Pacific
Mexico/BajaNorte

这篇关于从带有偏移值的日期获取时区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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