1 16 17 18 19 package org.apache.velocity.tools.generic; 20 21 22 import java.text.DateFormat ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Date ; 25 import java.util.Calendar ; 26 import java.util.Locale ; 27 import java.util.TimeZone ; 28 29 30 67 public class DateTool 68 { 69 70 75 public static final String DEFAULT_FORMAT = "default"; 76 77 80 public DateTool() 81 { 82 } 84 85 86 88 91 public static final Date getSystemDate() 92 { 93 return getSystemCalendar().getTime(); 94 } 95 96 97 100 public static final Calendar getSystemCalendar() 101 { 102 return Calendar.getInstance(); 103 } 104 105 106 108 116 public Locale getLocale() 117 { 118 return Locale.getDefault(); 119 } 120 121 129 public TimeZone getTimeZone() 130 { 131 return TimeZone.getDefault(); 132 } 133 134 139 public Date getDate() 140 { 141 return getCalendar().getTime(); 142 } 143 144 160 public Calendar getCalendar() 161 { 162 return Calendar.getInstance(getTimeZone(), getLocale()); 163 } 164 165 177 public String getFormat() 178 { 179 return DEFAULT_FORMAT; 180 } 181 182 183 185 189 public String getFormattedDate(String format) 190 { 191 return format(format, getDate()); 192 } 193 194 211 public String get(String format) 212 { 213 return format(format, getDate()); 214 } 215 216 228 public String get(String dateStyle, String timeStyle) 229 { 230 return format(dateStyle, timeStyle, getDate(), getLocale()); 231 } 232 233 234 243 public String format(Object obj) 244 { 245 return format(getFormat(), obj); 246 } 247 248 259 public String format(String format, Object obj) 260 { 261 return format(format, obj, getLocale()); 262 } 263 264 275 public String format(String format, Object obj, Locale locale) 276 { 277 return format(format, obj, locale, getTimeZone()); 278 } 279 280 334 public String format(String format, Object obj, 335 Locale locale, TimeZone timezone) 336 { 337 Date date = toDate(obj); 338 DateFormat df = getDateFormat(format, locale, timezone); 339 if (date == null || df == null) 340 { 341 return null; 342 } 343 return df.format(date); 344 } 345 346 347 358 public String format(String dateStyle, String timeStyle, Object obj) 359 { 360 return format(dateStyle, timeStyle, obj, getLocale()); 361 } 362 363 375 public String format(String dateStyle, String timeStyle, 376 Object obj, Locale locale) 377 { 378 return format(dateStyle, timeStyle, obj, locale, getTimeZone()); 379 } 380 381 395 public String format(String dateStyle, String timeStyle, 396 Object obj, Locale locale, TimeZone timezone) 397 { 398 Date date = toDate(obj); 399 DateFormat df = getDateFormat(dateStyle, timeStyle, locale, timezone); 400 if (date == null || df == null) 401 { 402 return null; 403 } 404 return df.format(date); 405 } 406 407 408 410 426 public DateFormat getDateFormat(String format, Locale locale, 427 TimeZone timezone) 428 { 429 if (format == null) 430 { 431 return null; 432 } 433 434 DateFormat df = null; 435 if (format.endsWith("_date")) 437 { 438 String fmt = format.substring(0, format.length() - 5); 439 int style = getStyleAsInt(fmt); 440 df = getDateFormat(style, -1, locale, timezone); 441 } 442 else if (format.endsWith("_time")) 444 { 445 String fmt = format.substring(0, format.length() - 5); 446 int style = getStyleAsInt(fmt); 447 df = getDateFormat(-1, style, locale, timezone); 448 } 449 else 451 { 452 int style = getStyleAsInt(format); 453 if (style < 0) 454 { 455 df = new SimpleDateFormat (format, locale); 457 df.setTimeZone(timezone); 458 } 459 else 460 { 461 df = getDateFormat(style, style, locale, timezone); 463 } 464 } 465 return df; 466 } 467 468 480 public DateFormat getDateFormat(String dateStyle, String timeStyle, 481 Locale locale, TimeZone timezone) 482 { 483 int ds = getStyleAsInt(dateStyle); 484 int ts = getStyleAsInt(timeStyle); 485 return getDateFormat(ds, ts, locale, timezone); 486 } 487 488 503 protected DateFormat getDateFormat(int dateStyle, int timeStyle, 504 Locale locale, TimeZone timezone) 505 { 506 try 507 { 508 DateFormat df; 509 if (dateStyle < 0 && timeStyle < 0) 510 { 511 df = DateFormat.getInstance(); 513 } 514 else if (timeStyle < 0) 515 { 516 df = DateFormat.getDateInstance(dateStyle, locale); 518 } 519 else if (dateStyle < 0) 520 { 521 df = DateFormat.getTimeInstance(timeStyle, locale); 523 } 524 else 525 { 526 df = DateFormat.getDateTimeInstance(dateStyle, timeStyle, 527 locale); 528 } 529 df.setTimeZone(timezone); 530 return df; 531 } 532 catch (Exception suppressed) 533 { 534 return null; 536 } 537 } 538 539 550 protected int getStyleAsInt(String style) 551 { 552 if (style == null || style.length() < 4 || style.length() > 7) { 554 return -1; 555 } 556 if (style.equalsIgnoreCase("full")) 557 { 558 return DateFormat.FULL; 559 } 560 if (style.equalsIgnoreCase("long")) 561 { 562 return DateFormat.LONG; 563 } 564 if (style.equalsIgnoreCase("medium")) 565 { 566 return DateFormat.MEDIUM; 567 } 568 if (style.equalsIgnoreCase("short")) 569 { 570 return DateFormat.SHORT; 571 } 572 if (style.equalsIgnoreCase("default")) 573 { 574 return DateFormat.DEFAULT; 575 } 576 return -1; 578 } 579 580 581 583 594 public Date toDate(Object obj) 595 { 596 return toDate(getFormat(), obj, getLocale(), getTimeZone()); 597 } 598 599 612 public Date toDate(String format, Object obj) 613 { 614 return toDate(format, obj, getLocale(), getTimeZone()); 615 } 616 617 629 public Date toDate(String format, Object obj, Locale locale) 630 { 631 return toDate(format, obj, locale, getTimeZone()); 632 } 633 634 648 public Date toDate(String format, Object obj, 649 Locale locale, TimeZone timezone) 650 { 651 if (obj == null) 652 { 653 return null; 654 } 655 if (obj instanceof Date ) 656 { 657 return (Date )obj; 658 } 659 if (obj instanceof Calendar ) 660 { 661 return ((Calendar )obj).getTime(); 662 } 663 if (obj instanceof Long ) 664 { 665 Date d = new Date (); 666 d.setTime(((Long )obj).longValue()); 667 return d; 668 } 669 try 670 { 671 DateFormat parser = getDateFormat(format, locale, timezone); 673 return parser.parse(String.valueOf(obj)); 674 } 675 catch (Exception e) 676 { 677 return null; 678 } 679 } 680 681 689 public Calendar toCalendar(Object obj) 690 { 691 return toCalendar(obj, getLocale()); 692 } 693 694 704 public Calendar toCalendar(Object obj, Locale locale) 705 { 706 if (obj == null) 707 { 708 return null; 709 } 710 if (obj instanceof Calendar ) 711 { 712 return (Calendar )obj; 713 } 714 Date date = toDate(obj); 716 if (date == null) 717 { 718 return null; 719 } 720 721 Calendar cal = Calendar.getInstance(locale); 723 cal.setTime(date); 724 cal.getTime(); 727 return cal; 728 } 729 730 731 733 738 public String toString() 739 { 740 return format(getFormat(), getDate()); 741 } 742 743 744 } 745 | Popular Tags |