更改 LocalDateTimeFormat 以包括秒和毫秒
Changing LocalDateTimeFormat to include seconds and milliseconds
根据要求,我的代码应该将 ZonedDateTime 参数中的日期和 OffSetTime 参数中的时间附加到这种格式中,"yyyy-MM-dd HH:mm:ss.SSSz"。但是,我没能做到这一点
我尝试了多种方法,包括下面使用 DateTimeFormatter 的方法。
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
LocalDateTime localDateTime = LocalDateTime.of(zonedDateTime.toLocalDate(), offsetTime.toLocalTime());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSz");
String finalDate = localDateTime.format(formatter);
我注意到:
- 代码在 localDateTime.format(格式化程序)
处抛出 "java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime"
期望像这样在字符串中获取日期时间 - “2019-05-23T08:59:00.000Z”
感谢任何帮助,感谢您抽出宝贵时间。
四期:
LocalDateTime
没有任何时区信息,所以不要使用它。
即使有,你的输入也只有一个时区offset,不是一个完整的时区,所以你格式化字符串不能使用z
,因为这需要时区 name。请改用 XXX
。
因为输入是偏移量 -05:00
你不应该期待输出 Z
(Zulu),因为这意味着 +00:00
,除非你也期待时间调整5小时
年的格式模式应该使用uuuu
,而不是yyyy
。参见
假设您想保留时区,将代码更改为:
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 08:59:00.000-05:00
如果您想要调整时间的祖鲁时间,即 UTC,请添加行以进行调整:
OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime)
.withOffsetSameInstant(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 13:59:00.000Z
根据要求,我的代码应该将 ZonedDateTime 参数中的日期和 OffSetTime 参数中的时间附加到这种格式中,"yyyy-MM-dd HH:mm:ss.SSSz"。但是,我没能做到这一点
我尝试了多种方法,包括下面使用 DateTimeFormatter 的方法。
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
LocalDateTime localDateTime = LocalDateTime.of(zonedDateTime.toLocalDate(), offsetTime.toLocalTime());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSz");
String finalDate = localDateTime.format(formatter);
我注意到: - 代码在 localDateTime.format(格式化程序)
处抛出 "java.time.DateTimeException: Unable to extract value: class java.time.LocalDateTime"期望像这样在字符串中获取日期时间 - “2019-05-23T08:59:00.000Z”
感谢任何帮助,感谢您抽出宝贵时间。
四期:
LocalDateTime
没有任何时区信息,所以不要使用它。即使有,你的输入也只有一个时区offset,不是一个完整的时区,所以你格式化字符串不能使用
z
,因为这需要时区 name。请改用XXX
。因为输入是偏移量
-05:00
你不应该期待输出Z
(Zulu),因为这意味着+00:00
,除非你也期待时间调整5小时年的格式模式应该使用
uuuu
,而不是yyyy
。参见
假设您想保留时区,将代码更改为:
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2019-05-23T09:00:00-05:00");
OffsetTime offsetTime = OffsetTime.parse("08:59:00-05:00");
OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 08:59:00.000-05:00
如果您想要调整时间的祖鲁时间,即 UTC,请添加行以进行调整:
OffsetDateTime offsetDateTime = zonedDateTime.toLocalDate().atTime(offsetTime)
.withOffsetSameInstant(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSSXXX");
System.out.println(offsetDateTime.format(formatter));
2019-05-23 13:59:00.000Z