Java:无法从TemporalAccessor获取LocalDate [英] Java: Unable to obtain LocalDate from TemporalAccessor
问题描述
我试图通过以下方式将String
日期的格式从EEEE MMMM d
更改为MM/d/yyyy
:首先将其转换为LocalDate
,然后将不同模式的格式化程序应用于LocalDate
再次将其解析为String
.
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 applying a formatter of a different pattern to the LocalDate
before parsing it into String
again.
这是我的代码:
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;
}
但是,我收到了我不太理解的异常消息:
However, I get this exception message that I don't really understand:
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)
推荐答案
正如其他答案所述,要创建LocalDate
,您需要 year ,该年份不在输入
As the other answers already said, to create a LocalDate
you need the year, which is not in the input String
. It has only day, month and day of the week.
要获取完整的LocalDate
,您需要解析日期和月份,并找到年份,其中天/月组合与一周中的某天匹配.
To get the full LocalDate
, you need to parse the day and month and find a year in which this day/month combination matches the day of the week.
当然,您可以忽略一周中的某天,并假定该日期始终为当前的年;在这种情况下,其他答案已经提供了解决方案.但是,如果要查找与一周中的某天完全匹配的年,则必须循环查找.
Of course you could ignore the day of the week and assume that the date is always in the current year; in this case, the other answers already provided the solution. But if you want to find the year that exactly matches the day of the week, you must loop until you find it.
我还要创建一个带有java.util.Locale
的格式化程序,以明确表示我想要英文的 month 和一周中的一天名称.如果您未指定语言环境,则它将使用系统的默认语言,并且不能保证始终使用英语(即使在运行时也可以不经通知即进行更改).
I'm also creating a formatter with a java.util.Locale
, to make it explicit that I want month and day of week names in English. If you don't specify a locale, it uses the system's default, and it's not guaranteed to always be English (and it can be changed without notice, even at runtime).
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("EEEE MMMM d"))
// use English Locale to correctly parse month and day of week
.toFormatter(Locale.ENGLISH);
// parse input
TemporalAccessor parsed = formatter.parse("TUESDAY JULY 25");
// get month and day
MonthDay md = MonthDay.from(parsed);
// get day of week
DayOfWeek dow = DayOfWeek.from(parsed);
LocalDate date;
// start with some arbitrary year, stop at some arbitrary value
for(int year = 2017; year > 1970; year--) {
// get day and month at the year
date = md.atYear(year);
// check if the day of week is the same
if (date.getDayOfWeek() == dow) {
// found: 'date' is the correct LocalDate
break;
}
}
在此示例中,我开始于2017年,试图找到一个直到1970年的日期,但是您可以适应适合您的用例的值.
In this example, I started at year 2017 and tried to find a date until back to 1970, but you can adapt to the values that fits your use cases.
您还可以使用Year.now().getValue()
获取当前年份(而不是某个固定的任意值).
You can also get the current year (instead of some fixed arbitrary value) by using Year.now().getValue()
.
这篇关于Java:无法从TemporalAccessor获取LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!