日历中的索引11将字符串中的月份打印为" 1月和#34;

时间:2016-12-14 09:31:44

标签: java calendar utc

转换jan 01 2017 00:00:00 NPT to utc时,结果为11(日历中的月份索引)31 2016 18:15:00这没关系。但是,如果我从索引11中提取月份作为字符串,它给我1月必须是12月 实际代码

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));

代码输出

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 个答案:

答案 0 :(得分:0)

你的代码输出:

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

对我来说是正确的