编写一方法:
public int test(int year,int month);
获取指定年月的最后一天,如year=2003,month=3,获取2003年3月的最后一天,返回值31

解决方案 »

  1.   

    d1.set(Calendar.YEAR, 2003);
            d1.set(Calendar.MONTH, 3);
            d1.set(Calendar.DAY_OF_MONTH, 0);
            
            System.out.println(d1.get(Calendar.DAY_OF_MONTH));
      

  2.   

    public int test(int year,int month) {
         Calendar cal = Calendar.getInstance();
         cal.set(year, month -1,1);
         cal.add(Calendar.MONTH,1);
         return cal.get(Calendar.DAY_OF_MONTH);
        }
      

  3.   

    参照二楼的,重改一下
    public int test(int year,int month) {
         Calendar cal = Calendar.getInstance();
         cal.set(year, month -1,0);
         return cal.get(Calendar.DAY_OF_MONTH);
        }
      

  4.   

    楼上month -1是错的改成:
    public static int test(int year,int month){
            Calendar cal = Calendar.getInstance();
         cal.set(year, month,0);
         return cal.get(Calendar.DAY_OF_MONTH);
        }
      

  5.   

    set
    public final void set(int year,
                          int month,
                          int date)
    Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH. Previous values of other calendar fields are retained. If this is not desired, call clear() first. Parameters:
    year - the value used to set the YEAR calendar field.
    month - the value used to set the MONTH calendar field. Month value is 0-based. e.g., 0 for January.
    date - the value used to set the DAY_OF_MONTH calendar field.=========================================================MONTH 是从0开始的,不应该减一么?但为什么减一的结果又是错误的呢?参数day设成0又是什么意思呢?