我从源系统获取以下字符串格式的日期时间内容以及偏移时间值。
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