【发布时间】:2018-01-01 10:09:20
【问题描述】:
我正在尝试将 String 日期的格式从 EEEE MMMM d 更改为 MM/d/yyyy,首先,将其转换为 LocalDate,然后将不同模式的格式化程序应用于之前的 LocalDate再次将其解析为String。
这是我的代码:
private String convertDate(String stringDate)
{
//from EEEE MMMM d -> MM/dd/yyyy
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
.toFormatter();
LocalDate parsedDate = LocalDate.parse(stringDate, formatter);
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("MM/d/yyyy");
String formattedStringDate = parsedDate.format(formatter2);
return formattedStringDate;
}
但是,我收到了这个我不太明白的异常消息:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'TUESDAY JULY 25' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {DayOfWeek=2, MonthOfYear=7, DayOfMonth=25},ISO of type java.time.format.Parsed
at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
【问题讨论】:
-
好吧,没有年份就不能约会。
-
你还没有说出你对这一年的期望。本年度?总是2017?根据月份和星期几的组合来计算年份?根据您的用例,KayV 或 Hugo 最接近。
标签: java java-time date-parsing localdate