I am trying to change the format of a String
date from EEEE MMMM d
to MM/d/yyyy
by, first, converting it into a LocalDate
and then在再次将其解析为 — 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)
原文由 user7049000 发布,翻译遵循 CC BY-SA 4.0 许可协议
正如其他答案已经说过的,要创建一个
LocalDate
你需要 年份,它不在输入中String
。它只有 day , month 和 day of the week 。要获得完整的
LocalDate
,您需要解析 日期 和 月份 并找到该 日期/月份 组合与 星期几 匹配的 年份。当然,您可以忽略 星期几,并假设日期始终是 当年;在这种情况下,其他答案已经提供了解决方案。但是如果你想找到与 星期几 完全匹配的 年份,你必须循环直到找到它。
我还创建了一个带有
java.util.Locale
的格式化程序,以明确表示我想要英文的 月份 和 星期几 名称。如果您不指定语言环境,它将使用系统的默认设置,并且不能保证始终是英语(并且可以在不通知的情况下更改,即使在运行时也是如此)。在此示例中,我从 2017 年开始并试图找到一个可以追溯到 1970 年的日期,但您可以调整适合您的用例的值。
您还可以使用
Year.now().getValue()
获取当前年份(而不是某个固定的任意值)。