小编Pri*_*iti的帖子

DateTime("2019-08-07T19:20-5:00") 转换为带有偏移值的本地日期时间

我从源系统获取以下字符串格式的日期时间内容以及偏移时间值。

2019-08-07T19:20-5:00
Run Code Online (Sandbox Code Playgroud)

我想使用偏移值将其转换为日期时间。我尝试了以下方法,但没有得到预期的结果。

OffsetDateTime sourceDateTime = OffsetDateTime.parse("2019-08-07T19:20-5:00"); 
System.out.println(sourceDateTime.getOffset());
Run Code Online (Sandbox Code Playgroud)

输出

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-08-07T19:20-5:00' could not be parsed at index 16
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
    at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:387)
    at com.example.demo.Testfile.main(Testfile.java:19)
Run Code Online (Sandbox Code Playgroud)

有什么简单的方法可以达到预期的输出吗?

注意:我使用的是Java 8

java java-8 java-time

3
推荐指数
1
解决办法
231
查看次数

标签 统计

java ×1

java-8 ×1

java-time ×1