| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.lang; |
| 22 |
|
| 23 |
import java.util.Date; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public class DateUtil |
| 29 |
{ |
| 30 |
private static int[] _daysInMonth365 = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private static final long EPOCH_1_JAN_1904 = -2082844800000L; |
| 37 |
|
| 38 |
public static boolean isValidDate(int year, int month, int day) |
| 39 |
{ |
| 40 |
if (year < 1 || year > 9999 || month < 0 || month > 11) |
| 41 |
return false; |
| 42 |
|
| 43 |
int daysInMonth = _daysInMonth365[month]; |
| 44 |
if (month == 1) |
| 45 |
{ |
| 46 |
boolean isLeapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); |
| 47 |
if (isLeapYear) |
| 48 |
daysInMonth++; |
| 49 |
} |
| 50 |
|
| 51 |
return day >= 1 && day <= daysInMonth; |
| 52 |
} |
| 53 |
|
| 54 |
public static boolean isValidTime(int hours, int minutes, int seconds) |
| 55 |
{ |
| 56 |
return hours >= 0 && hours < 24 |
| 57 |
&& minutes >= 0 && minutes < 60 |
| 58 |
&& seconds >= 0 && seconds < 60; |
| 59 |
} |
| 60 |
|
| 61 |
public static Date get1Jan1904EpochDate(long seconds) |
| 62 |
{ |
| 63 |
return new Date((seconds * 1000) + EPOCH_1_JAN_1904); |
| 64 |
} |
| 65 |
} |