我想將字符串轉換爲即時。你能幫我嗎?解析字符串時間戳即時拋出不支持的字段:InstantSeconds
我得到以下異常:
產生的原因:java.time.temporal.UnsupportedTemporalTypeException:不支持的領域:InstantSeconds 在java.time.format.Parsed.getLong(Parsed.java:203) 在java.time.Instant.from(Instant.java:373)
我的代碼基本上是這樣的
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String timestamp = "2016-02-16 11:00:02";
TemporalAccessor temporalAccessor = formatter.parse(timestamp);
Instant result = Instant.from(temporalAccessor);
我正在使用Java 8 Update 72.
你的第一個輸入是不是一瞬間。時區丟失。 –
我得到這個錯誤試圖使用DateTimeFormatter.ofLocalizedDateTime 2-arg的構造函數,其中我可以在互聯網上找到0個例子。它不喜歡將FormatStyle.SHORT作爲timeFormat 2nd參數傳遞給方法。 – djangofan