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)
Here is the minor change which you need to implement:
Add the default chronological year in the formatter using
.parseDefaulting(ChronoField.YEAR, 2017)
Call the method using the argument
"Tuesday July 25"
like thisconvertDate("Tuesday July 25");
As the other answers already said, to create a
LocalDate
you need the year, which is not in the inputString
. 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).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()
.The documentation for
LocalDate
says, thatIn your case, the input
String
is missing an important component ofLocalDate
, i.e the year. What you have basically is month and day. So, you can use a class suited to thatMonthDay
. Using that your code can be modified to :Another option is to do the following (just like the other answers a bit hacky), assuming of course you want the date to fall in the current year: