1 package com.icl.saxon.exslt; 2 import java.util.Calendar ; 3 import java.util.GregorianCalendar ; 4 import java.text.SimpleDateFormat ; 5 6 10 11 public final class Date { 12 13 16 17 private Date() {}; 18 19 28 29 public static String dateTime() { 30 Calendar calendar = new GregorianCalendar (); 31 int tzoffset = calendar.get(Calendar.ZONE_OFFSET) + 32 calendar.get(Calendar.DST_OFFSET); 33 char sign = '+'; 34 if (tzoffset < 0) { 35 sign = '-'; 36 tzoffset = -tzoffset; 37 } 38 int tzminutes = tzoffset / 60000; 39 int tzhours = tzminutes / 60; 40 tzminutes = tzminutes % 60; 41 String tzh = "" + tzhours; 42 while (tzh.length() < 2) tzh = "0" + tzh; 43 String tzm = "" + tzminutes; 44 while (tzm.length() < 2) tzm = "0" + tzm; 45 46 SimpleDateFormat formatter 47 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss"); 48 String base = formatter.format(new java.util.Date ()); 49 return base + sign + tzh + ':' + tzm; 50 } 51 52 57 58 public static String date(String dateTime) { 59 int offset = 0; 60 if (dateTime.length() >= 1 && 61 (dateTime.charAt(0) == '-' || dateTime.charAt(0) == '+')) { 62 offset = 1; 63 } 64 if (dateTime.length() >= offset+10) { 65 return dateTime.substring(0, offset+10); 66 } else { 67 return ""; 68 } 69 } 70 71 74 75 public static String date() { 76 return date(dateTime()); 77 } 78 79 84 85 public static String time(String dateTime) { 86 int t = dateTime.indexOf('T'); 87 if (t<0 || t==dateTime.length()-1) { 88 return ""; 89 } else { 90 return dateTime.substring(t+1); 91 } 92 } 93 94 97 98 public static String time() { 99 return time(dateTime()); 100 } 101 102 107 108 public static double year(String dateTime) { 109 if (dateTime.startsWith("-")) { 110 return Double.NaN; 111 } 112 try { 113 return (double)Integer.parseInt(dateTime.substring(0, 4)); 114 } catch (Exception err) { 115 return Double.NaN; 116 } 117 } 118 119 122 123 public static double year() { 124 return year(dateTime()); 125 } 126 127 131 132 public static boolean leapYear(String dateTime) { 133 double year = year(dateTime); 134 if (Double.isNaN(year)) { 135 return false; 136 } 137 int y = (int)year; 138 return (y % 4 == 0) && !((y % 100 == 0) && !(y % 400 == 0)); 139 } 140 141 144 145 public static boolean leapYear() { 146 return leapYear(dateTime()); 147 } 148 149 153 154 public static double monthInYear(String dateTime) { 155 try { 156 if (dateTime.startsWith("--")) { 157 return (double)Integer.parseInt(dateTime.substring(2, 4)); 158 } else { 159 if (dateTime.indexOf('-')!=4) { 160 return Double.NaN; 161 } 162 return (double)Integer.parseInt(dateTime.substring(5, 7)); 163 } 164 } catch (Exception err) { 165 return Double.NaN; 166 } 167 } 168 169 172 173 public static double monthInYear() { 174 return monthInYear(date()); 175 } 176 177 182 183 public static String monthName(String date) { 184 String [] months = {"January", "February", "March", "April", "May", "June", 185 "July", "August", "September", "October", "November", "December"}; 186 double m = monthInYear(date); 187 if (Double.isNaN(m)) { 188 return ""; 189 } 190 return months[(int)m - 1]; 191 } 192 193 197 198 public static String monthName() { 199 return monthName(date()); 200 } 201 202 207 208 public static String monthAbbreviation(String date) { 209 String [] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 210 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; 211 double m = monthInYear(date); 212 if (Double.isNaN(m)) { 213 return ""; 214 } 215 return months[(int)m - 1]; 216 } 217 218 222 223 public static String monthAbbreviation() { 224 return monthAbbreviation(date()); 225 } 226 227 231 232 public static double weekInYear(String dateTime) { 233 int dayInYear = (int)dayInYear(dateTime); 234 int weekDay = ((int)dayInWeek(dateTime) + 5) % 7; 235 String firstJan = dateTime.substring(0,4) + "-01-01"; 237 int jan1day = ((int)dayInWeek(firstJan) + 5) % 7; 238 int daysInFirstWeek = (jan1day==0 ? 0 : 7 - jan1day); 239 240 int rawWeek = (dayInYear - daysInFirstWeek + 6) / 7; 241 242 if (daysInFirstWeek >= 4) { 243 return rawWeek + 1; 244 } else { 245 if (rawWeek>0) { 246 return rawWeek; 247 } else { 248 int lastYear = Integer.parseInt(dateTime.substring(0,4)) - 1; 250 String dec31 = lastYear + "-12-31"; 251 return weekInYear(dec31); 253 } 254 } 255 } 256 257 261 262 public static double weekInYear() { 263 return weekInYear(date()); 264 } 265 266 270 271 public static double weekInMonth(String dateTime) { 272 return (double)(int)((dayInMonth(dateTime)-1) / 7 + 1); 274 } 275 276 279 280 public static double weekInMonth() { 281 return weekInMonth(date()); 282 } 283 284 287 288 public static double dayInYear(String dateTime) { 289 int month=(int)monthInYear(dateTime); 290 int day = (int)dayInMonth(dateTime); 291 int[] prev = { 0, 292 31, 293 31+28, 294 31+28+31, 295 31+28+31+30, 296 31+28+31+30+31, 297 31+28+31+30+31+30, 298 31+28+31+30+31+30+31, 299 31+28+31+30+31+30+31+31, 300 31+28+31+30+31+30+31+31+30, 301 31+28+31+30+31+30+31+31+30+31, 302 31+28+31+30+31+30+31+31+30+31+30, 303 31+28+31+30+31+30+31+31+30+31+30+31 }; 304 int leap = (month>2 && leapYear(dateTime) ? 1 : 0); 305 return prev[month-1] + leap + day; 306 } 307 308 311 312 public static double dayInYear() { 313 return dayInYear(date()); 314 } 315 316 320 321 public static double dayInMonth(String dateTime) { 322 try { 323 if (dateTime.startsWith("---")) { 324 return (double)Integer.parseInt(dateTime.substring(3,5)); 325 } else if (dateTime.startsWith("--")) { 326 return (double)Integer.parseInt(dateTime.substring(5,7)); 327 } else { 328 return (double)Integer.parseInt(dateTime.substring(8,10)); 329 } 330 } catch (Exception err) { 331 return Double.NaN; 332 } 333 } 334 335 338 339 public static double dayInMonth() { 340 return dayInMonth(date()); 341 } 342 343 348 349 public static double dayOfWeekInMonth(String dateTime) { 350 double dd = dayInMonth(dateTime); 351 if (Double.isNaN(dd)) { 352 return dd; 353 } 354 return (((int)dd) - 1) / 7 + 1; 355 } 356 357 361 362 public static double dayOfWeekInMonth() { 363 return dayOfWeekInMonth(date()); 364 } 365 366 372 373 public static double dayInWeek(String dateTime) { 374 double yy = year(dateTime); 375 double mm = monthInYear(dateTime); 376 double dd = dayInMonth(dateTime); 377 if (Double.isNaN(yy) || Double.isNaN(mm) || Double.isNaN(dd)) { 378 return Double.NaN; 379 } 380 GregorianCalendar calDate = 381 new GregorianCalendar ( 382 (int)yy, 383 (int)mm-1, 384 (int)dd); 385 calDate.setFirstDayOfWeek(Calendar.SUNDAY); 386 return calDate.get(Calendar.DAY_OF_WEEK); 387 } 388 389 394 395 public static double dayInWeek() { 396 return dayInWeek(date()); 397 } 398 399 404 405 public static String dayName(String dateTime) { 406 String [] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 407 "Saturday" }; 408 double d = dayInWeek(dateTime); 409 if (Double.isNaN(d)) { 410 return ""; 411 } 412 return days[(int)d - 1]; 413 } 414 415 419 420 public static String dayName() { 421 return dayName(date()); 422 } 423 424 429 430 public static String dayAbbreviation(String dateTime) { 431 String [] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 432 double d = dayInWeek(dateTime); 433 if (Double.isNaN(d)) { 434 return ""; 435 } 436 return days[(int)d - 1]; 437 } 438 439 443 444 public static String dayAbbreviation() { 445 return dayAbbreviation(date()); 446 } 447 448 452 453 public static double hourInDay(String dateTime) { 454 int t = dateTime.indexOf('T'); 455 try { 456 int hh = Integer.parseInt(dateTime.substring(t+1, t+3)); 457 return (double)hh; 458 } catch (Exception err) { 459 return Double.NaN; 460 } 461 } 462 463 466 467 public static double hourInDay() { 468 return hourInDay(time()); 469 } 470 471 475 476 public static double minuteInHour(String dateTime) { 477 int t = dateTime.indexOf('T'); 478 try { 479 int mm = Integer.parseInt(dateTime.substring(t+4, t+6)); 480 return (double)mm; 481 } catch (Exception err) { 482 return Double.NaN; 483 } 484 } 485 486 489 490 public static double minuteInHour() { 491 return minuteInHour(time()); 492 } 493 494 498 499 public static double secondInMinute(String dateTime) { 500 int t = dateTime.indexOf('T'); 501 try { 502 int ss = Integer.parseInt(dateTime.substring(t+7, t+9)); 503 return (double)ss; 504 } catch (Exception err) { 505 return Double.NaN; 506 } 507 } 508 509 512 513 public static double secondInMinute() { 514 return secondInMinute(time()); 515 } 516 517 } 518 519 520 521 522 523 | Popular Tags |