0

This is my Java code

Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

c.set(Calendar.YEAR, 2015);
c.set(Calendar.MONTH, 11);
c.set(Calendar.DAY_OF_MONTH, 31);
c.set(Calendar.HOUR_OF_DAY, 22);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-YYYY HH:mm:ss");
System.out.println("date  is : "+sdf.format(c.getTime()));

c.add(Calendar.HOUR_OF_DAY, 1);

System.out.println("date2  is : "+sdf.format(c.getTime()));

and here is the output

date  is : 31-Dec-2015 23:00:00
date2  is : 01-Jan-2015 00:00:00

Why is the year not changing to 2016 in the 2nd print statement?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Your code doesn't fit the output. It returns "date is : 31-Dec-2015 22:00:00" and "date2 is : 31-Dec-2015 23:00:00" – Felix D. Dec 08 '15 at 08:41

1 Answers1

1

I tried your code and it is working as you need, but you should change the format to

dd-MMM-yyyy HH:mm:ss
teobais
  • 2,820
  • 1
  • 24
  • 36
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97