Java 日历月字段未更新

Java 日历月字段未更新,java,calendar,Java,Calendar,我有代码,我必须在其中找到月份,所以我们在其中添加了月份。gettime()方法显示更新的时间,但月份字段未更新。在下面的示例中,更改的月份与当前月份相同 Calendar cal = Calendar.getInstance(); System.out.println("Current time is "+ cal.getTime()); System.out.println("Current month is "+Calendar.MONTH); cal.add(Calendar.MONTH

我有代码,我必须在其中找到月份,所以我们在其中添加了月份。gettime()方法显示更新的时间,但月份字段未更新。在下面的示例中,更改的月份与当前月份相同

Calendar cal = Calendar.getInstance();
System.out.println("Current time is "+ cal.getTime());
System.out.println("Current month is "+Calendar.MONTH);
cal.add(Calendar.MONTH, -2);

System.out.println("Changed time is "+ cal.getTime());
System.out.println("Changed month is "+Calendar.MONTH);
以上代码的输出是 当前时间为2016年2月22日星期一17:59:13 本月为2月 更改时间为2015年12月22日星期二17:59:13 更改的月份是2


我不想使用calendar类中不推荐的方法来获取月数。月数是一个常量整数。你想要的是:

int month = cal.get(Calendar.MONTH); // zero-based!!!
System.out.println("Month=" + (month + 1));

Calendar.MONTH
是一个常量整数。你想要的是:

int month = cal.get(Calendar.MONTH); // zero-based!!!
System.out.println("Month=" + (month + 1));

Calendar.MONTH
定义为
2
,用于在日历中查找所需字段。在搜索结果中未获得该字段
Calendar.MONTH
定义为
2
,用于在日历中查找所需字段。在搜索结果中未获得该字段