无法将格式化字符串转换为 LocalDateTime
Unable to Convert Formatted String to LocalDateTime
我正在尝试使用以下代码将字符串 Fri August 16 2019 12:08:55 AM
转换为 LocalDateTime
对象:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMMM d YYYY h:mm:ss a", Locale.US);
String timestamp = "Fri August 16 2019 12:08:55 AM";
localDateTime = LocalDateTime.parse(timestamp, formatter);
此代码抛出以下异常:
java.time.format.DateTimeParseException: Text 'Fri August 16 2019 12:08:55 AM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2019, MonthOfYear=8, DayOfWeek=5, DayOfMonth=16},ISO resolved to 00:08:55 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at suppliers.pojos.PriceFluctuationPOJO.<init>(PriceFluctuationPOJO.java:51)
at suppliers.pojos.PriceFluctuationPOJO.readFromPriceFluctuationCSVFile(PriceFluctuationPOJO.java:163)
at amzn.Main.main(Main.java:60)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2019, MonthOfYear=8, DayOfWeek=5, DayOfMonth=16},ISO resolved to 00:08:55 of type java.time.format.Parsed
at java.time.LocalDateTime.from(Unknown Source)
at java.time.format.Parsed.query(Unknown Source)
基于 SO 上的 this and this 线程,格式似乎是正确的。
什么导致异常?
谢谢
您的输入中不应包含单引号 String
,并且您的模式已关闭。你想要 yyyy
(而不是 YYYY
)。喜欢,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMMM d yyyy hh:mm:ss a", Locale.US);
String timestamp = "Fri August 16 2019 12:08:55 AM";
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, formatter);
System.out.println(localDateTime);
输出(此处)
2019-08-16T00:08:55
我正在尝试使用以下代码将字符串 Fri August 16 2019 12:08:55 AM
转换为 LocalDateTime
对象:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMMM d YYYY h:mm:ss a", Locale.US);
String timestamp = "Fri August 16 2019 12:08:55 AM";
localDateTime = LocalDateTime.parse(timestamp, formatter);
此代码抛出以下异常:
java.time.format.DateTimeParseException: Text 'Fri August 16 2019 12:08:55 AM' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2019, MonthOfYear=8, DayOfWeek=5, DayOfMonth=16},ISO resolved to 00:08:55 of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalDateTime.parse(Unknown Source)
at suppliers.pojos.PriceFluctuationPOJO.<init>(PriceFluctuationPOJO.java:51)
at suppliers.pojos.PriceFluctuationPOJO.readFromPriceFluctuationCSVFile(PriceFluctuationPOJO.java:163)
at amzn.Main.main(Main.java:60)
Caused by: java.time.DateTimeException: Unable to obtain LocalDateTime from TemporalAccessor: {WeekBasedYear[WeekFields[SUNDAY,1]]=2019, MonthOfYear=8, DayOfWeek=5, DayOfMonth=16},ISO resolved to 00:08:55 of type java.time.format.Parsed
at java.time.LocalDateTime.from(Unknown Source)
at java.time.format.Parsed.query(Unknown Source)
基于 SO 上的 this and this 线程,格式似乎是正确的。
什么导致异常?
谢谢
您的输入中不应包含单引号 String
,并且您的模式已关闭。你想要 yyyy
(而不是 YYYY
)。喜欢,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMMM d yyyy hh:mm:ss a", Locale.US);
String timestamp = "Fri August 16 2019 12:08:55 AM";
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, formatter);
System.out.println(localDateTime);
输出(此处)
2019-08-16T00:08:55