Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat("dd-MMM-YYYY") printing year one year ahead [duplicate]

Tags:

java

I am using SimpleDateFormat("dd-MMM-YYYY") in my code, which is giving wrong output.

    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-YYYY");

    System.out.println("Actual date : "+new Date()+" after Formatting : "+ dateFormat.format(new Date()));

above code is giving : Actual date : Tue Dec 30 13:51:06 IST 2014 after Formatting : 30-Dec-2015

Above code is print date having Year as 1 year ahead. and this issue is replicable for 28-31 december 2014 dates only.

Thanks in advance. --Ajay

like image 491
AjGupta Avatar asked Jan 09 '23 04:01

AjGupta


1 Answers

You're using YYYY, which is the "ISO-8601 week year". That should almost always be used in conjunction with w, "week in year". You want yyyy to show the normal calendar year.

The reason they're different is that the first ISO-8601 week of a year is the first (Monday to Sunday) week that includes at least 4 days. This means that the first week of the year is the one containing the first Thursday. As January 1st 2015 falls on a Thursday, that means that the week of 2014-12-29 to 2015-01-04 is all "week year 2015, week of year 1". (I'm surprised if you see if for December 28th...)

In other years, the first few days of the year are in week 52 or 53 of the previous year. For example, January 1st 2010 was in week 53 of week-year 2009, and January 1st 2011 was in week 52 of week-year 2010.

like image 69
Jon Skeet Avatar answered Jan 31 '23 23:01

Jon Skeet