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.
Here's my code:
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)
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.
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.
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;
}
}
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.
You can also get the current year (instead of some fixed arbitrary value) by using Year.now().getValue()
.