日期:2014-05-20  浏览次数:20705 次

关于calendar
public class CalendarTest
{
public static void main(String[] args)
{
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
System.out.println(month);
}
}


为什么打印出来的月份是11不是12月?

------解决方案--------------------
d.get(Calendar.MONTH)取值为0-11
------解决方案--------------------
你把电脑系统时间改成1月,再执行代码你就知道了
------解决方案--------------------


楼主查下API
------解决方案--------------------
取法的问题,看下源码
/**
     * Field number for <code>get</code> and <code>set</code> indicating the
     * month. This is a calendar-specific value. The first month of
     * the year in the Gregorian and Julian calendars is
     * <code>JANUARY</code> which is 0; the last depends on the number
     * of months in a year.
     *
     * @see #JANUARY
     * @see #FEBRUARY
     * @see #MARCH
     * @see #APRIL
     * @see #MAY
     * @see #JUNE
     * @see #JULY
     * @see #AUGUST
     * @see #SEPTEMBER
     * @see #OCTOBER
     * @see #NOVEMBER
     * @see #DECEMBER
     * @see #UNDECIMBER
     */
    public final static int MONTH = 2;