1 package net.sf.saxon.exslt; 2 import java.text.SimpleDateFormat ; 3 import java.util.Calendar ; 4 import java.util.GregorianCalendar ; 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 String firstJan = dateTime.substring(0,4) + "-01-01"; 235 int jan1day = ((int)dayInWeek(firstJan) + 5) % 7; 236 int daysInFirstWeek = (jan1day==0 ? 0 : 7 - jan1day); 237 238 int rawWeek = (dayInYear - daysInFirstWeek + 6) / 7; 239 240 if (daysInFirstWeek >= 4) { 241 return rawWeek + 1; 242 } else { 243 if (rawWeek>0) { 244 return rawWeek; 245 } else { 246 int lastYear = Integer.parseInt(dateTime.substring(0,4)) - 1; 248 String dec31 = lastYear + "-12-31"; 249 return weekInYear(dec31); 251 } 252 } 253 } 254 255 259 260 public static double weekInYear() { 261 return weekInYear(date()); 262 } 263 264 268 269 public static double weekInMonth(String dateTime) { 270 return (double)(int)((dayInMonth(dateTime)-1) / 7 + 1); 271 } 272 273 276 277 public static double weekInMonth() { 278 return weekInMonth(date()); 279 } 280 281 284 285 public static double dayInYear(String dateTime) { 286 int month=(int)monthInYear(dateTime); 287 int day = (int)dayInMonth(dateTime); 288 int[] prev = { 0, 289 31, 290 31+28, 291 31+28+31, 292 31+28+31+30, 293 31+28+31+30+31, 294 31+28+31+30+31+30, 295 31+28+31+30+31+30+31, 296 31+28+31+30+31+30+31+31, 297 31+28+31+30+31+30+31+31+30, 298 31+28+31+30+31+30+31+31+30+31, 299 31+28+31+30+31+30+31+31+30+31+30, 300 31+28+31+30+31+30+31+31+30+31+30+31 }; 301 int leap = (month>2 && leapYear(dateTime) ? 1 : 0); 302 return prev[month-1] + leap + day; 303 } 304 305 308 309 public static double dayInYear() { 310 return dayInYear(date()); 311 } 312 313 317 318 public static double dayInMonth(String dateTime) { 319 try { 320 if (dateTime.startsWith("---")) { 321 return (double)Integer.parseInt(dateTime.substring(3,5)); 322 } else if (dateTime.startsWith("--")) { 323 return (double)Integer.parseInt(dateTime.substring(5,7)); 324 } else { 325 return (double)Integer.parseInt(dateTime.substring(8,10)); 326 } 327 } catch (Exception err) { 328 return Double.NaN; 329 } 330 } 331 332 335 336 public static double dayInMonth() { 337 return dayInMonth(date()); 338 } 339 340 345 346 public static double dayOfWeekInMonth(String dateTime) { 347 double dd = dayInMonth(dateTime); 348 if (Double.isNaN(dd)) { 349 return dd; 350 } 351 return (((int)dd) - 1) / 7 + 1; 352 } 353 354 358 359 public static double dayOfWeekInMonth() { 360 return dayOfWeekInMonth(date()); 361 } 362 363 369 370 public static double dayInWeek(String dateTime) { 371 double yy = year(dateTime); 372 double mm = monthInYear(dateTime); 373 double dd = dayInMonth(dateTime); 374 if (Double.isNaN(yy) || Double.isNaN(mm) || Double.isNaN(dd)) { 375 return Double.NaN; 376 } 377 GregorianCalendar calDate = 378 new GregorianCalendar ( 379 (int)yy, 380 (int)mm-1, 381 (int)dd); 382 calDate.setFirstDayOfWeek(Calendar.SUNDAY); 383 return calDate.get(Calendar.DAY_OF_WEEK); 384 } 385 386 391 392 public static double dayInWeek() { 393 return dayInWeek(date()); 394 } 395 396 401 402 public static String dayName(String dateTime) { 403 String [] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 404 "Saturday" }; 405 double d = dayInWeek(dateTime); 406 if (Double.isNaN(d)) { 407 return ""; 408 } 409 return days[(int)d - 1]; 410 } 411 412 416 417 public static String dayName() { 418 return dayName(date()); 419 } 420 421 426 427 public static String dayAbbreviation(String dateTime) { 428 String [] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; 429 double d = dayInWeek(dateTime); 430 if (Double.isNaN(d)) { 431 return ""; 432 } 433 return days[(int)d - 1]; 434 } 435 436 440 441 public static String dayAbbreviation() { 442 return dayAbbreviation(date()); 443 } 444 445 449 450 public static double hourInDay(String dateTime) { 451 int t = dateTime.indexOf('T'); 452 try { 453 int hh = Integer.parseInt(dateTime.substring(t+1, t+3)); 454 return (double)hh; 455 } catch (Exception err) { 456 return Double.NaN; 457 } 458 } 459 460 463 464 public static double hourInDay() { 465 return hourInDay(time()); 466 } 467 468 472 473 public static double minuteInHour(String dateTime) { 474 int t = dateTime.indexOf('T'); 475 try { 476 int mm = Integer.parseInt(dateTime.substring(t+4, t+6)); 477 return (double)mm; 478 } catch (Exception err) { 479 return Double.NaN; 480 } 481 } 482 483 486 487 public static double minuteInHour() { 488 return minuteInHour(time()); 489 } 490 491 495 496 public static double secondInMinute(String dateTime) { 497 int t = dateTime.indexOf('T'); 498 try { 499 int ss = Integer.parseInt(dateTime.substring(t+7, t+9)); 500 return (double)ss; 501 } catch (Exception err) { 502 return Double.NaN; 503 } 504 } 505 506 509 510 public static double secondInMinute() { 511 return secondInMinute(time()); 512 } 513 514 } 515 516 517 518 519 520 | Popular Tags |