尝试使用Calendar对象获取特定月份的天数

时间:2012-09-08 00:47:42

标签: java calendar

我正在尝试在java中编写一个Calendar,在表格中输出一个html网页,其中包含Calendar,但我在尝试时遇到了问题获取特定年份特定月份的天数。

这是我正在使用的代码:

    //accept input from command prompt in form of MONTH, DAY, YEAR
    String date = args[0];
    SimpleDateFormat df = new SimpleDateFormat("MMMM dd, yyyy");
    Date convertedDate = new Date();
    try
    {
        convertedDate = df.parse(date);
    }
    catch(Exception e)
    {
        System.out.print(e);
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(convertedDate);
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);



    //get number of days in month
    int numDays, startMonth;
    numDays = cal.getActualMaximum(DAY_OF_MONTH);

我从最后一行看到的错误是:

error: cannot find symbol and it points to the DAY_OF_MONTH variable.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

使用Calendar.DAY_OF_MONTH

numDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

这是Calendar类的静态字段。

相关问题