看看isLeapYear和getMonthMaxDays函数
==============================================================
package dragonpc.utils;
import java.util.Calendar;
/** 日期处理工具函数集
* @see dragonpc.jcomponent.DgDateComboBox
* @see dragonpc.jcomponent.DgDatePanel
* @see dragonpc.jcomponent.dialog.DgDatePickerDlg
*/
public class DgDateTimeUtils {
/** Private Contruct Method */
private DgDateTimeUtils() {
}
/** 判断是否为闰年
*
* @param year
*
* @return 返回输入的年份是否为闰年
* */
public final static boolean isLeapYear(int year) {
return ((year % 4 == 0) &&
((year % 100 != 0) || (year % 400 == 0)));
}
/** 得到指定日期是星期几
*
* @param year 年
* @param month 月
* @param day 日
*
* @return 0: Sunday, 1: Monday, 2: Tuesday ... 6: Saturday
*/
public final static int getDayOfWeek(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day);
return cal.get(cal.DAY_OF_WEEK) - 1;
}
/** 得到指定月份可能的最大天数
*
* @param year
* @param month
*
* @return 大月返回 31,小月返回 30,闰年的二月返回 29,非闰年的二月返回 28
*
* @see #isLeapYear
*/
public final static int getMonthMaxDays(int year, int month) {
switch (month) {
case 4 :
case 6 :
case 9 :
case 11 :
return 30;
case 2 :
if (isLeapYear(year))
return 29;
else
return 28;
default : // case 1, 3, 5, 7, 8, 10, 12:
return 31;
}
}
/** @return 获取当前系统的 year 值 */
public final static int getCurrentYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
/** 获取当前系统的 month 值
*
* @return 0, 1, 2, ... 11
*/
public final static int getCurrentMonth() {
return Calendar.getInstance().get(Calendar.MONTH);
}
/** @return 获取当前系统的 day 值,以 1 开始计算 */
public final static int getCurrentDay() {
return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
}
/** 得到当前日期是星期几
*
* @return 0: Sunday, 1: Monday, 2: Tuesday ... 6: Saturday
*/
public final static int getCurrentDayOfWeek() {
return Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
}
/** 获得当前时间的描述
*
* @see dragonpc.util.DgDebug
* @see dragonpc.util.DgStringUtils
*
* @return 当前日期和时间,用于调试
*/
public final static String getNow() {
Calendar cal = Calendar.getInstance();
int nYear = cal.get(Calendar.YEAR);
int nMonth = cal.get(Calendar.MONTH);
int nDay = cal.get(Calendar.DAY_OF_MONTH);
int nHour = cal.get(Calendar.HOUR_OF_DAY);
int nMin = cal.get(Calendar.MINUTE);
int nSec = cal.get(Calendar.SECOND);
int nMSec = cal.get(Calendar.MILLISECOND);
return DgStringUtils.formatNumber(nHour, 2) + ":" + DgStringUtils.formatNumber(nMin, 2) + ":" + DgStringUtils.formatNumber(nSec, 2) + "-" + DgStringUtils.formatNumber(nMSec, 3);
}
}