0

I'm using Java 8 on linux with the following code

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
LocalDate exampleDate  = LocalDate.parse(myDate, formatter);

where myDate is a String equal to "150520". I'm getting error:

java.time.format.DateTimeParseException: Text '150520' could not 
be parsed: Conflict found: Field MonthOfYear 1 differs from 
MonthOfYear 5 derived from 2015-01-20

I'd like to return May 20, 2015 for example. Any idea what's wrong?

UPDATE

Replacing the date code D with d eliminates this error. How to format into readable date?

user46688
  • 733
  • 3
  • 11
  • 29
  • More duplicates: [this](http://stackoverflow.com/q/36172920/642706) and [this](http://stackoverflow.com/q/40388834/642706) and [this](http://stackoverflow.com/q/10732297/642706) and [this](http://stackoverflow.com/q/251535/642706). – Basil Bourque May 10 '17 at 23:47

3 Answers3

5

You want yyMMdd. The uppercase D parses "day of year" not "day of month".

[Edit] for the printing part, you could do DateTimeFormatter.ofPattern("MMM dd, yyyy").format(theDate).

jingx
  • 3,698
  • 3
  • 24
  • 40
2

Change

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);

with

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd",Locale.ENGLISH);

Spock
  • 315
  • 2
  • 13
2

You need to use this format string "yyMMdd". According to the docs

Symbol  Meaning                     Presentation      Examples
------  -------                     ------------      -------
D       day-of-year                 number            189
d       day-of-month                number            10
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82