1 3 package jodd.datetime; 4 5 11 public class TimeUtil { 12 13 public static final long MILLIS_IN_DAY = 1000L * 60 * 60 * 24; 14 15 17 23 public static int dayOfYear(int year, int month, int day) { 24 int day_of_year; 25 if (isLeapYear(year)) { 26 day_of_year = ((275 * month) / 9) - ((month + 9) / 12) + day - 30; 27 } else { 28 day_of_year = ((275 * month) / 9) - (((month + 9) / 12) << 1) + day - 30; 29 } 30 return day_of_year; 31 } 32 33 34 39 public static boolean isLeapYear(int y) { 40 boolean result = false; 41 42 if (((y % 4) == 0) && ((y < 1582) || ((y % 100) != 0) || ((y % 400) == 0))) { result = true; } 48 return result; 49 } 50 51 private static final int[] MONTH_LENGTH = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 52 private static final int[] LEAP_MONTH_LENGTH = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 53 54 60 public static int getMonthLength(int year, int m) { 61 if ((m < 1) || (m > 12)) { 62 return -1; 63 } 64 if (isLeapYear(year)) { 65 return LEAP_MONTH_LENGTH[m]; 66 } 67 return MONTH_LENGTH[m]; 68 } 69 70 71 73 78 public static boolean isValidDate(int year, int month, int day) { 79 if ((month < 1) || (month > 12)) { 80 return false; 81 } 82 int ml = getMonthLength(year, month); 83 if ((day < 1) || (day > ml)) { 85 return false; 86 } 87 return true; 88 } 89 90 99 public static boolean isValidTime(int hour, int minute, double second) { 100 if ((hour < 0) || (hour >= 24)) { 101 return false; 102 } 103 if ((minute < 0) || (minute >= 60)) { 104 return false; 105 } 106 if ((second < 0) || (second >= 60)) { 108 return false; 109 } 110 return true; 111 } 112 113 125 public static boolean isValidDateTime(int year, int month, int day, int hour, int minute, double second) { 126 return (isValidDate(year, month, day) && isValidTime(hour, minute, second)); 127 } 128 129 136 public static boolean isValidDateTime(DateTimeStamp dts) { 137 return (isValidDate(dts.year, dts.month, dts.day) && isValidTime(dts.hour, dts.minute, dts.second)); 138 } 139 140 141 143 148 public static JulianDateStamp toJulianDate(DateTimeStamp time) { 149 return toJulianDate(time.year, time.month, time.day, time.hour, time.minute, time.second); 150 } 151 152 172 public static JulianDateStamp toJulianDate(int year, int month, int day, int hour, int minute, double second) { 173 JulianDateStamp jds = new JulianDateStamp(); 175 176 if ((month > 12) || (month < -12)) { 178 month--; 179 int delta = month / 12; 180 year += delta; 181 month -= delta * 12; 182 month++; 183 } 184 if (month < 0) { 185 year--; 186 month += 12; 187 } 188 189 double frac = (hour / 24.0) + (minute / 1440.0) + (second / 86400.0); 191 if (frac < 0) { int delta = ((int)(-frac)) + 1; 193 frac += delta; 194 day -= delta; 195 } 196 double gyr = year + (0.01 * month) + (0.0001 * day) + (0.0001 * frac) + 1.0e-9; 197 198 int iy0; 200 int im0; 201 if (month <= 2) { 202 iy0 = year - 1; 203 im0 = month + 12; 204 } else { 205 iy0 = year; 206 im0 = month; 207 } 208 int ia = iy0 / 100; 209 int ib = 2 - ia + (ia >> 2); 210 211 int jd; 213 if (year <= 0) { 214 jd = (int)((365.25 * iy0) - 0.75) + (int)(30.6001 * (im0 + 1)) + day + 1720994; 215 } else { 216 jd = (int)(365.25 * iy0) + (int)(30.6001 * (im0 + 1)) + day + 1720994; 217 } 218 if (gyr >= 1582.1015) { jd += ib; 220 } 221 224 jds.integer = jd; 225 jds.fraction = frac + 0.5; 226 return jds; 227 } 228 229 230 237 public static DateTimeStamp fromJulianDate(double JD) { 238 return fromJulianDate(new JulianDateStamp(JD)); 239 } 240 241 242 251 public static DateTimeStamp fromJulianDate(JulianDateStamp jds) { 252 DateTimeStamp time = new DateTimeStamp(); 253 int year, month, day; 254 double frac; 255 int jd, ka, kb, kc, kd, ke, ialp; 256 double d_hour, d_minute; 257 258 262 ka = (int)(jds.fraction + 0.5); 263 jd = jds.integer + ka; 264 frac = jds.fraction + 0.5 - ka + 1e-10; 265 266 ka = jd; 267 if (jd >= 2299161) { 268 ialp = (int)(((double)jd - 1867216.25) / (36524.25)); 269 ka = jd + 1 + ialp - (ialp >> 2); 270 } 271 kb = ka + 1524; 272 kc = (int)(((double)kb - 122.1) / 365.25); 273 kd = (int)((double)kc * 365.25); 274 ke = (int)((double)(kb - kd) / 30.6001); 275 day = kb - kd - ((int)((double)ke * 30.6001)); 276 if (ke > 13) { 277 month = ke - 13; 278 } else { 279 month = ke - 1; 280 } 281 if ((month == 2) && (day > 28)){ 282 day = 29; 283 } 284 if ((month == 2) && (day == 29) && (ke == 3)) { 285 year = kc - 4716; 286 } else if (month > 2) { 287 year = kc - 4716; 288 } else { 289 year = kc - 4715; 290 } 291 time.year = year; 292 time.month = month; 293 time.day = day; 294 295 d_hour = frac * 24.0; 297 time.hour = (int)d_hour; 299 d_minute = (d_hour - (double)time.hour) * 60.0; 301 time.minute = (int)d_minute; 303 time.second = (d_minute - (double)time.minute) * 60.0; 304 305 time.second = ((int) (time.second * 10000) + 0.5) / 10000.0; 307 return time; 308 } 309 310 311 } 312 | Popular Tags |