Java8时间API 生成带时区的时间

通过LocalDateTime.now()生成的时间 转换成yyyy-MM-dd'T'HH:mm:ss.SSSXXX 带时区的格式会报Unsupported field: OffsetSeconds异常,把LocalDateTime换成ZonedDateTime即可。

// 代码
public static void main(String[] args) {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMAT_DATETIME_IOS8601)));
}

// 异常

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds
at java.time.LocalDate.get0(LocalDate.java:680)
at java.time.LocalDate.getLong(LocalDate.java:659)
at java.time.LocalDateTime.getLong(LocalDateTime.java:720)
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298)
at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3335)
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2179)
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746)
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720)
at java.time.LocalDateTime.format(LocalDateTime.java:1752)
at com.zhx.cdppurchase.util.DateUtil.main(DateUtil.java:43)

Process finished with exit code 1

 

posted @ 2021-01-26 15:02  屈小舒  阅读(3012)  评论(0编辑  收藏  举报