0

I would like to parse a date format YYYY-MM-DD into the following MMM dd, yyyy. This is my part of code which is causing DateTimeParseException - >

DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("MMM dd, yyyy"); LocalDate localDate = LocalDate.parse("2002-10-01",dateTimeFormatter1);

The exception message when I try to parse the date with the given format is: Method threw 'java.time.format.DateTimeParseException' exception. Text '2002-10-01' could not be parsed at index 0.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • Your formatter is for the output pattern. You need 2 formatters. One to parse, and one to output. You can't just use 1 formatter and expect it to read your mind about what you're trying to do. – Michael May 21 '21 at 15:49

2 Answers2

3

That's because 2002-10-01 is in format YYYY-MM-DD, so trying to parse (i.e. convert it to a LocalDate object) it with format MMM dd, yyyy isn't going to work.

You need two formats to do this:

DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("MMM dd, yyyy");

LocalDate localDate = LocalDate.parse("2002-10-01",dateTimeFormatter1);
String newRepresentation = dateTimeFormatter2.format(localDate);

System.out.println(newRepresentation);
Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
  • I had no problems by getting an string object for the format MMM dd, yyyy. The problem is I would need to have it as an LocalDate object in MMM dd, yyyy format . So for the following example I would need to have Oct 01, 2002 of LocalDate object. Kind regards – Jasmin Jasko Merušić May 21 '21 at 15:09
  • 1
    @JasminJaskoMerušić That sounds like you expect the `LocalDate` object to have knowledge of how it should be formatted. It does not (and should not). The whole point of `DateTimeFormatter` is to convert date/time objects to strings, and vice versa. It's the `DateTimeFormatter` that knows how to parse and format, not the date/time objects. – Slaw May 21 '21 at 18:26
3

Your exception was caused by your specified parsing format "MMM dd, yyyy" not matching the input 2002-10-01.

You commented:

I would need to have it as an LocalDate object in MMM dd, yyyy format

A LocalDate does not have a “format”. Text has a format, but LocalDate is not text.

The LocalDate class can parse an incoming string to produce a LocalDate object. And a LocalDate object can generate a string that represents its value. But a LocalDate object itself is neither of those strings. LocalDate has its own internal representation of a date, the details of which do not concern us.

You said:

I would like to parse a date format YYYY-MM-DD

That format complies with the ISO 8601 standard. The java.time classes use those standard formats by default when parsing/generating text. So no need to specify a formatting pattern.

LocalDate ld = LocalDate.parse( "2002-10-01" ) ;

You said

… into the following MMM dd, yyyy.

Generally better to let java.time automatically localize rather than you hard-code a specific format.

Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = ld.format( f ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154