static int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
/**
* @param date yyyy-MM-dd HH:mm:ss
* @return
*/
public static boolean isValidDate(String date) {
try {
int year = Integer.parseInt(date.substring(0, 4));
if (year <= 0)
return false;
int month = Integer.parseInt(date.substring(5, 7));
if (month <= 0 || month > 12)
return false;
int day = Integer.parseInt(date.substring(8, 10));
if (day <= 0 || day > DAYS[month])
return false;
if (month == 2 && day == 29 && !isGregorianLeapYear(year)) {
return false;
}
int hour = Integer.parseInt(date.substring(11, 13));
if (hour < 0 || hour > 23)
return false;
int minute = Integer.parseInt(date.substring(14, 16));
if (minute < 0 || minute > 59)
return false;
int second = Integer.parseInt(date.substring(17, 19));
if (second < 0 || second > 59)
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static final boolean isGregorianLeapYear(int year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
public static void main(String[] args) {
System.out.println(isValidDate("2100-02-29 23:00:01"));
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。