当前页面: 开发资料首页 → J2ME 专题 → 为何calendar取得的当前月份会少一个月??
为何calendar取得的当前月份会少一个月??
摘要: 为何calendar取得的当前月份会少一个月??
代码如下:
Calendar rightNow = Calendar.getInstance();
int month = rightNow.get(Calendar.MONTH);
不知为何,month总是比当前月份小1,比如现在是11月,取得的month为10,我将系统设置为1月份,取得的month为0。在模拟器和真机上都是这样
但年和日均正常
请大伙帮帮忙,点醒我一下,是什么原因哪?
public static final int MONTHField number for get and set indicating the month. This is a calendar-specific value. The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.
jdk的说明。
就是这样子的,就跟数组下标从0开始一样,没有为什么
没什么原因,就是这样的。
这个问题有的时候也是会爆发的,所以有的时候要稍微包装一下日期类。
你看这个例子
-----------------------------------------------------------
Calendar rightNow = Calendar.getInstance();
Date date = new Date(82,6,3);
rightNow.setTime(date);
int month = rightNow.get(Calendar.MONTH);
System.out.println(month);
String format = "MM/dd/yyyy HH:mm:ss";
SimpleDateFormat formatter = new SimpleDateFormat(format);
System.out.println(formatter.format(rightNow.getTime()));
--------------------------------------------------------------------
打印结果是
6
07/03/1982 00:00:00
也就是说我传的date类型1982年6月3号
Calendar类型把他认作1982年7月3号了
他非要把数字6认为7月,没办法。。。。。。
就是这样的
谢谢大伙!! :)
我也猜想过可能是系统这样设定的,但在jdk中没有找到gzpoplar(poplar) 提供的说明的后半部份(The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.)
你用我贴上来的那段句子到google或baidu试试。