Rak*_*esh 5 java datetime timezone-offset java-8
我有一个日期时间字符串"2018-01-15 01:16:00",它位于EST时区.我想使用UTC偏移动态地将其转换为另一个时区.我的javascript代码将此UTC偏移量作为参数传递,并且servlet必须将此日期时间字符串转换/格式化为由提供的偏移量标识的时区.
我尝试了很多方法,包括oracle教程中记录的方法,但无法找到解决方案.
以下是我正在尝试的代码,非常感谢任何帮助.
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final String DEFAULT_TIME_ZONE = ZoneId.SHORT_IDS.get("EST");
public static void main(String[] args) throws Exception {
String dateTime = "2018-01-15 02:35:00";
//parse the datetime using LocalDateTime
LocalDateTime defaultDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATE_FORMAT));
//get the datetime in default timezone
ZoneId defaultZoneId = ZoneId.of(DEFAULT_TIME_ZONE);
ZonedDateTime defaultZoneDateTime = defaultDateTime.atZone(defaultZoneId);
System.out.println("EST time: "+defaultZoneDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT)));
ZonedDateTime utcZonedDateTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
String utcTime = defaultZoneDateTime.withZoneSameInstant(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern(DATE_FORMAT));
System.out.println("UTC : "+utcTime);
//IST timezone
ZoneOffset offset = ZoneOffset.of("+05:30");
OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);
String targetTimeZone = offsetDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT));
System.out.printf("target time : "+targetTimeZone);
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT
EST time: 2018-01-15 02:35:00
UTC : 2018-01-15 07:37:00
target time : 2018-01-15 07:37:00
Run Code Online (Sandbox Code Playgroud)
预计目标时间: 2018-01-15 13:05:00
Jon*_*eet 13
直接问题是这一行:
OffsetDateTime offsetDate = OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset);
Run Code Online (Sandbox Code Playgroud)
这就是说你想要相同的本地日期/时间,但是要有指定的偏移量.这改变了哪个时刻正在被表现出来.
相反,你真的想要代表同一时刻,但在特定的偏移量.所以最短的修复是:
OffsetDateTime offsetDate = utcZonedDateTime.toInstant().atOffset(offset);
Run Code Online (Sandbox Code Playgroud)
但是,还有许多其他方面可以改变:
ZoneOffset.UTC到ZoneId.of("UTC")EST时区令人困惑 - 目前尚不清楚您是否期望它意味着"东部时间"(在EST和EDT之间变化)或纯标准时间UTC-5.假设您实际上意味着"东部时间",那么最好将其America/New_York用作区域ID.接下来,您根本不需要将ZonedDateTime东部时间转换为ZonedDateTimeUTC格式.将其直接转换为瞬间:
OffsetDateTime target = defaultZoneDateTime.toInstant().at(offset);
Run Code Online (Sandbox Code Playgroud)
或者ZonedDateTime为目标创建一个:
ZonedDateTime target = defaultZoneDateTime.withZoneSameInstant(offset);
Run Code Online (Sandbox Code Playgroud)
鉴于偏移不是真正的时区,我可能会选择其中的第一个.
你正在使用
OffsetDateTime.of(utcZonedDateTime.toLocalDateTime(), offset)
Run Code Online (Sandbox Code Playgroud)
创建目标.因此,您在目标偏移量中构造一个OffsetDateTime,其LocalDateTime等于UTC区域中的LocalDateTime.
您想要的是与您从EST时间到UTC时使用的完全相同的转换:保持相同的瞬间,但转到不同的时区:
defaultZoneDateTime.withZoneSameInstant(offset);
Run Code Online (Sandbox Code Playgroud)
或者,如果你真的想要一个OffsetDateTime而不是ZonedDateTime:
OffsetDateTime.ofInstant(defaultZoneDateTime.toInstant(), offset);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1509 次 |
| 最近记录: |