calendar.get(Calendar.MONTH)
如果是1月,返回的是0。它下标从0开始,所以转到实际的要加1。下面这段代码,就看出来了,如果是12月,c.get(Calendar.MONTH)返回的是11。String s = "20121201";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(s);
Calendar c = Calendar.getInstance();
c.setTime(date); System.out.println(c.get(Calendar.MONTH));
如果是1月,返回的是0。它下标从0开始,所以转到实际的要加1。下面这段代码,就看出来了,如果是12月,c.get(Calendar.MONTH)返回的是11。String s = "20121201";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse(s);
Calendar c = Calendar.getInstance();
c.setTime(date); System.out.println(c.get(Calendar.MONTH));
就是一个习惯问题吧,好像就只有Calendar类是这样的,其他时间类是从1开始的。
LZ好好读读JDK API就知道原因了public static final int MONTH
Field 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. 注意红色的部分,Calendar的月份是从0开始计算的,0代表January,以此类推6代表July即我们的7月份。
good~