[英]Format String Date from dd-MM-YYYY (Unparseable date)
我在這里遇到問題,給出了日期的格式(dd-MM-YYYY),這不是一個很好的格式(我知道),但這是原始數據,我無法使用SimpleDateFormat
類來解析字符串,它給出了一個錯誤
java.text.ParseException: Unparseable date: "27-08-2019"
而且我還使用了DateTimeFormatter
但同樣無法解析,這是錯誤。
java.time.format.DateTimeParseException: Text '19-08-2019' could not be parsed at index 0
因此,我的工作選擇是首先格式化String
以便日期格式化程序可以解析字符串日期,但是如果有其他選擇來最大程度地減少對String
格式數據的操作,那將是不錯的選擇。 謝謝。
編輯
抱歉,您未發布代碼。 此代碼使用SimpleDateFormat
String string = "08-27-2019";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
在這里使用DateTimeFormatter
和LocalDate
。
String string = "08-27-2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(date);
感謝所有人的幫助,指出了我的錯誤后,我開始努力了。 我只是用給定的格式解析它,然后將解析日期格式化為我想要的格式。
String string = "27-08-2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy", Locale.ENGLISH);
LocalDate date = LocalDate.parse(string, formatter);
System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH).format(date));
如果有更多指向我解決方案的指示,我很樂意接受任何更正。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.