java.util.Calendar thisMonth = java.util.Calendar.getInstance();
thisMonth.set(Calendar.MONTH, 9);
thisMonth.set(Calendar.YEAR, 2001);
int maxIndex=thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
maxIndex即是最后一天的号。

解决方案 »

  1.   

    我试了,可Calendar中没有方法getActualMaximum,而有getMaximum,但它返回的值不是我需要的,你用的是java哪个版本?我用的是1.2
      

  2.   

    里面有这个方法
    getActualMaximumpublic int getActualMaximum(int field)
    Return the maximum value that this field could have, given the current date. For example, with the date "Feb 3, 1997" and the DAY_OF_MONTH field, the actual maximum would be 28; for "Feb 3, 1996" it s 29. Similarly for a Hebrew calendar, for some years the actual maximum for MONTH is 12, and for others 13. The version of this function on Calendar uses an iterative algorithm to determine the actual maximum value for the field. There is almost always a more efficient way to accomplish this (in most cases, you can simply return getMaximum()). GregorianCalendar overrides this function with a more efficient implementation.Parameters:field - the field to determine the maximum ofReturns:the maximum of the given field for the current date of this CalendarSince: 1.2
      

  3.   

    可以这样:
    java.util.Calendar thisMonth = java.util.Calendar.getInstance();
    thisMonth.set(Calendar.MONTH, 9+1);
    thisMonth.set(Calendar.YEAR, 2001);
    thisMonth.set(Calendar.Date,-1);
    int maxIndex = thisMonth.get(Calendar.Date);
      

  4.   

    请问  binriyue(日月)  ,  XKP(低等下人) 你们用的是什么java开发工具?我的java中的确没有找到方法getActualMaximum!
    谢谢knight_qmh(辉),我也试过,可Calendar中没有Date 变量
      

  5.   

    好像大家的JDK没什么不一样的吧?
    如果那个方法用不了那就算了
    你用knight_qmh(辉)
    的也可以啊
    不过他的那个Date忘了大写
    你改一下试试看吧
      

  6.   

    试试我的程序:
    import java.util.*;public class CalendarTest { public static void main(String[] args) {
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);

    d.set(Calendar.DAY_OF_MONTH, 1);
    int weekday = d.get(Calendar.DAY_OF_WEEK);

    System.out.println("Sun Mon Tue Wed Thu Fri Sat");

    for (int i = Calendar.SUNDAY; i < weekday; i++ )
              System.out.print("    ");
             
    do
    {
    int day = d.get(Calendar.DAY_OF_MONTH);
    if (day < 10) System.out.print(" ");
    System.out.print(day);

    if (day == today)
    System.out.print("* ");
    else
    System.out.print("  "); 

    if (weekday == Calendar.SATURDAY)
    System.out.println();

    d.add(Calendar.DAY_OF_MONTH, 1);    weekday = d.get(Calendar.DAY_OF_WEEK);
    }while (d.get(Calendar.MONTH) == month);

    if (weekday != Calendar.SUNDAY)
    System.out.println();       
    }
    }
    基本思路是:采用循环定义一个日期模式变量:GregorianCalendar d = new GregorianCalendar(),让他每次加一:d.add(Calendar.DAY_OF_MONTH, 1); 当不是本月分时推出循环,那么那个循环累加器就是月初,在减1就是了。
      

  7.   

    日期减一的方法是:d.add(Calendar.DAY_OF_MONTH, -1);