亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java.time.format.DateTimeParseException:

java.time.format.DateTimeParseException:

躍然一笑 2022-09-07 16:56:55
我在將字符串格式化為ZonedDateTime時遇到問題。我的客戶希望日期采用 ddMMyyhhmmss 等格式,沒有分隔符或類似的東西。這是我到目前為止所做的import java.time.format.DateTimeFormatter;import java.time.LocalDateTime;import java.time.ZoneId;import java.time.ZonedDateTime;public class MyClass {    public static void main(String args[]) {            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("ddMMyyyyhhmmss");            String test = formatter            .format(ZonedDateTime.now()).toString();            System.out.println(test);            ZonedDateTime a = ZonedDateTime.parse(test,formatter);            System.out.println(a.toString());    }}當它正確生成字符串時,錯誤發生在創建 LocalDateTime 變量的解析過程中28032019100707Exception in thread "main" java.time.format.DateTimeParseException: Text '28032019100707' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {MilliOfSecond=0, MinuteOfHour=7, HourOfAmPm=10, NanoOfSecond=0, MicroOfSecond=0, SecondOfMinute=7},ISO resolved to 2019-03-28 of type java.time.format.Parsed    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)    at java.time.ZonedDateTime.parse(ZonedDateTime.java:597)    at MyClass.main(MyClass.java:14)在SO上搜索,我看到同一問題的一些答案建議使用LocalDateTime類作為中間類,然后解析為ZonedDateTime,但它仍然不起作用,拋出了相同的錯誤。我還嘗試使用此過程更改初始化DateTimeFormatter的方式DateTimeFormatter formatter = new DateTimeFormatterBuilder().appendPattern("ddMMyyyyhhmmss")                          .toFormatter()                          .withZone(ZoneId.systemDefault());但它仍然不起作用。我知道我肯定錯過了一些愚蠢的東西,但我看不到什么。任何人都可以給我指出正確的方向嗎?
查看完整描述

3 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

你想要:

  String test = ZonedDateTime.now().format(formatter);


查看完整回答
反對 回復 2022-09-07
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

ddMMyyyyhhmmss不包含任何區域信息,它不是,它應該是 。ZonedDateTimeLocalDateTime



查看完整回答
反對 回復 2022-09-07
?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

在您解析的字符串中,沒有關于時區的信息 - 那么格式化程序應該如何知道如何轉換為ZonedDateTime?

您需要在字符串中包含時區信息,或者使用另一個對象將其解析為沒有時區信息,然后將其傳輸到所需的區域中。


查看完整回答
反對 回復 2022-09-07
  • 3 回答
  • 0 關注
  • 441 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號