Index 11 from Calendar prints months in string as "January"

79 Views Asked by At

While converting jan 01 2017 00:00:00 NPT to utc, the result is 11(month index in Calendar) 31 2016 18:15:00 which is okay. But if i extract the month as string from index 11, it gives me January which must be December.
Actual code

Date date = new Date(1483208100000L);
System.out.println("date: " + date);

System.out.println("\nUTC with month as index::::");
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.setTime(date);

System.out.println("Year: " + calendar.get(Calendar.YEAR) + ", month: " + calendar.get(Calendar.MONTH) + ", day: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("hours: " + calendar.get(Calendar.HOUR_OF_DAY) + ", minutes: " + calendar.get(Calendar.MINUTE) + ", seconds: " + calendar.get(Calendar.SECOND));


System.out.println("\nUTC with month as String::::");     
String month = new SimpleDateFormat("MMMM").format(calendar.getTime());       
System.out.println("Year: " + calendar.get(Calendar.YEAR) + ", month: " + month + ", day: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("hours: " + calendar.get(Calendar.HOUR_OF_DAY) + ", minutes: " + calendar.get(Calendar.MINUTE) + ", seconds: " + calendar.get(Calendar.SECOND));

Output of code

date: Sun Jan 01 00:00:00 NPT 2017

UTC with month as index::::
Year: 2016, month: 11, day: 31
hours: 18, minutes: 15, seconds: 0

UTC with month as String::::
Year: 2016, month: January, day: 31
hours: 18, minutes: 15, seconds: 0
1

There are 1 best solutions below

2
On

your code output this:

date: Sat Dec 31 18:15:00 GMT 2016

UTC with month as index::::
Year: 2016, month: 11, day: 31
hours: 18, minutes: 15, seconds: 0

UTC with month as String::::
Year: 2016, month: December, day: 31
hours: 18, minutes: 15, seconds: 0

which looks correct to me