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); }
参照二楼的,重改一下 public int test(int year,int month) { Calendar cal = Calendar.getInstance(); cal.set(year, month -1,0); return cal.get(Calendar.DAY_OF_MONTH); }
楼上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); }
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又是什么意思呢?
d1.set(Calendar.MONTH, 3);
d1.set(Calendar.DAY_OF_MONTH, 0);
System.out.println(d1.get(Calendar.DAY_OF_MONTH));
Calendar cal = Calendar.getInstance();
cal.set(year, month -1,1);
cal.add(Calendar.MONTH,1);
return cal.get(Calendar.DAY_OF_MONTH);
}
public int test(int year,int month) {
Calendar cal = Calendar.getInstance();
cal.set(year, month -1,0);
return cal.get(Calendar.DAY_OF_MONTH);
}
public static int test(int year,int month){
Calendar cal = Calendar.getInstance();
cal.set(year, month,0);
return cal.get(Calendar.DAY_OF_MONTH);
}
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又是什么意思呢?