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即是最后一天的号。
thisMonth.set(Calendar.MONTH, 9);
thisMonth.set(Calendar.YEAR, 2001);
int maxIndex=thisMonth.getActualMaximum(Calendar.DAY_OF_MONTH);
maxIndex即是最后一天的号。
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
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);
谢谢knight_qmh(辉),我也试过,可Calendar中没有Date 变量
如果那个方法用不了那就算了
你用knight_qmh(辉)
的也可以啊
不过他的那个Date忘了大写
你改一下试试看吧
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就是了。