Reputation: 36
I'm trying to parse dates which come in format of "1 March 2019" or eg "15 March 2019" in LocalDateTime format but I'm struggling with finding the correct pattern for it.
I've tried the example with pattern "dd M/L yyyy" and simply "d M/L yyyy" but none seem to work.
String begda = "1 March 2019";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd M/L yyyy");
LocalDateTime ldtBeg = LocalDateTime.parse(begda, formatter);
It will just throw an DateTimeParseException stating that it could not get parsed.
Upvotes: 0
Views: 638
Reputation: 28289
Use MMMM
for the complete month like March
. And use d
for day like 1
.
It should like d MMMM yyyy
.
And specify the Locale
for the human language to use in reading the name of month.
LocalDate.parse(
"1 March 2019" ,
DateTimeFormatter.ofPattern(
"d MMMM yyyy",
Locale.UK
)
)
Upvotes: 5
Reputation:
you are looking for converting String month MARCH
. So, while parsing you need to mention of which Locale it is.
while formatting this locale is being used and respective month for that is returned.
If you are familiar with Calendar then you can use that as well. I guess this link can give you some hint
Convert Month String to Integer in Java
Upvotes: 0
Reputation: 483
It's because you're doing it wrong
the correct date format for java is ("dd MMMM yyyy")
and for more information visit this site
Upvotes: 0