0

I have the following code and want to set cal to 01.01.2016.

DateFormat dateFormat = new SimpleDateFormat("dd.MM.YYYY");
Calendar cal = Calendar.getInstance();
cal.set(2016, Calendar.JANUARY, 1);
System.out.println(dateFormat.format(cal.getTime()));

But it always return 01.01.2015. I tried to set year to 2017, it outputs 2016. For 2015, 2014 and 2018, it works correctly.

ArcticLord
  • 3,999
  • 3
  • 27
  • 47

1 Answers1

0

The year in format must be an lower case y

DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");

Upper case Y is for Week year.

For more details see the java doc of SimpleDateFormat

Jens
  • 67,715
  • 15
  • 98
  • 113