6
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, YYYY, EEE", Locale.US);

System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));

Java does not parse the date as expected and outputs:

Apr 27, 2018, Fri
Jan 05, 2018, Fri // I can not understand why java parse the month of April as January
Karol Dowbecki
  • 38,744
  • 9
  • 58
  • 89
user9709261
  • 147
  • 2
  • 7
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated along with `Date`, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Apr 27 '18 at 09:18

4 Answers4

7

Your problem is that you are using YYYY, which represents Week year, whereas you wanted yyyy which represents year, see all options here.

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, EEE", Locale.US);
achAmháin
  • 3,944
  • 3
  • 12
  • 39
3

As per SimpleDateFormat Javadoc

y    Year    
Y    Week year  

So you want to use yyyy instead YYYY:

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, EEE", Locale.US);

System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));

will result int:

Apr 27, 2018, Fri
Apr 27, 2018, Fri

Do note that you probably want to set sdf.setLenient(false) when using SimpleDateFormat.

Karol Dowbecki
  • 38,744
  • 9
  • 58
  • 89
3

As explained in the other answers, what you want is yyyy instead of YYYY .

Now to add further information about the resut you get :

Date String : "Apr 27, 2018, Fri"

Format : "MMM dd, YYYY, EEE" (YYYY being week year)

Quoting from How does Java “week year” really work?

On parsing, SimpleDateFormat expects a matching set of values: either day, month, year or day of week, week in year, week-year. Since you supplied a week-year but did not supply day of week and week in year, those to values have been assumed as 1.

You don't have the week in year part (or your format would contain w), so it is assumed to be 1 .

You end up having friday of the first week of the year 2018, which is :

Jan 05, 2018, Fri

(The day and the month from your date get discarded once the format hits the YYYY part, and are replaced by the result of the "year week" computing. )

Arnaud
  • 16,319
  • 3
  • 24
  • 39
0

Y stands for week year

From Java doc

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

For example, January 1, 1998 is a Thursday. If getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4 (ISO 8601 standard compatible setting), then week 1 of 1998 starts on December 29, 1997, and ends on January 4, 1998. The week year is 1998 for the last three days of calendar year 1997. If, however, getFirstDayOfWeek() is SUNDAY, then week 1 of 1998 starts on January 4, 1998, and ends on January 10, 1998; the first three days of 1998 then are part of week 53 of 1997 and their week year is 1997.

pavithraCS
  • 606
  • 6
  • 22