我对java 8日期格式/解析功能有点沮丧.我试图找到杰克逊配置并DateTimeFormatter解析 "2018-02-13T10:20:12.120+0000"字符串到任何Java 8日期,并没有找到它.
这是java.util.Date一个很好的例子:
Date date = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZZZ")
.parse("2018-02-13T10:20:12.120+0000");
Run Code Online (Sandbox Code Playgroud)
相同的格式不适用于新的日期时间api
ZonedDateTime dateTime = ZonedDateTime.parse("2018-02-13T10:20:12.120+0000",
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss.SSSZZZ"));
Run Code Online (Sandbox Code Playgroud)
我们应该能够以适合FE UI应用程序的任何格式格式化/解析日期.也许我误解或错误的东西,但我认为java.util.Date提供更多的格式灵活性和更容易使用.
我正在使用ZonedDateTime和Java 8的DateTimeFormatter.当我尝试解析自己的模式时,它无法识别并抛出异常.
String oraceDt = "1970-01-01 00:00:00.0";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
ZonedDateTime dt = ZonedDateTime.parse(oraceDt, formatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '1970-01-01 00:00:00.0' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 1970-01-01T00:00 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1918)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1853)
at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)
at com.timezone.Java8DsteTimes.testZonedDateTime(Java8DsteTimes.java:31)
at com.timezone.Java8DsteTimes.main(Java8DsteTimes.java:11)
Run Code Online (Sandbox Code Playgroud)
引起:java.time.DateTimeException:无法从TemporalAccessor获取ZonedDateTime:{},ISO解析为1970-01-01T00:00,类型为java.time.format.Parsed