我正在尝试将日期转换为以下时区,但结果不符合预期 - 我得到的要求是例如从PMST to EST输出应该少2小时。
PMST,
新标准时间,
谷草转氨酶,
美东时间,
国家标准时间,
MST,
太平洋标准时间,
农业科学技术,
哈斯特
String inputDate = "2017/04/30 08:10";
DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
LocalDateTime local = LocalDateTime.parse(inputDate, sourceFormatter);
ZonedDateTime zoned = local.atZone(TimeZone.getTimeZone("PMST").toZoneId());
ZonedDateTime requiredZone = zoned.withZoneSameInstant(TimeZone.getTimeZone("EST").toZoneId());
System.out.println(requiredZone);
输出- 2017-04-30T03:10-05:00
避免伪区域
切勿使用媒体上常见的 3-4 个字母缩写,例如CST
, IST
, and EST
。这些都是不是真正的时区,不是标准化的,甚至不是唯一的(!)。
真实时区:continent/region
相反,请确定您想要的真实时区。时区有名称 https://en.m.wikipedia.org/wiki/List_of_tz_database_time_zones格式为continent/region
例如America/Montreal
and Africa/Casablanca
and Pacific/Auckland
.
更改您的输入以符合 ISO 8601 标准格式。
String input = "2017/04/30 08:10".replace( " " , "T" ) ;
解析为LocalDateTime
因为您的输入缺少与 UTC 或时区的偏移量指示符。
LocalDateTime ldt = LocalDateTime.parse( input ) ;
如果您确定该输入适用于时区,则应用时区。
ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = ldt.withZoneSameInstant( z ) ;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)