0
我使用ZonedDateTime與Java 8的DateTimeFormatter。當我嘗試解析我自己的模式時,它不識別並拋出異常。Java8- ZonedDateTime與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)
產生的原因:java.time.DateTimeException:無法從TemporalAccessor獲得ZonedDateTime:{},ISO決心1970-01-01T00:00型java.time.format.Parsed
感謝Meno ..我去選項1,因爲我需要通過時區。創建ZoneId對象並傳遞給它。 ZoneId zoneId = ZoneId.of(ZoneId.SHORT_IDS.get(「CST」)); – Venkat