org.apache.velocity.tools.generic
Class DateTool

java.lang.Object
  |
  +--org.apache.velocity.tools.generic.DateTool

public class DateTool
extends java.lang.Object

Tool for working with Date and Calendar in Velocity templates. This tool is entirely threadsafe, and has no instance members. As such, the methods are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default calendar, locale, or timezone.

Version:
$Revision: 1.3 $ $Date: 2003/07/04 05:13:10 $
Author:
Nathan Bubna

Constructor Summary
DateTool()
          Default constructor.
 
Method Summary
 java.lang.String format(java.lang.String format, java.lang.Object obj)
          Converts the specified object to a date and returns a formatted string representing that date using the locale returned by getLocale().
static java.lang.String format(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
          Returns a formatted string representing the specified date and locale.
 java.util.Calendar getCalendar()
          Returns a Calendar instance created using the timezone and locale returned by getTimeZone() and getLocale().
 java.util.Date getDate()
          Returns a Date derived from the result of getCalendar()
 java.lang.String getFormattedDate(java.lang.String format)
          Returns a formatted string representing the date as returned by getDate().
 java.util.Locale getLocale()
           
static java.util.Calendar getSystemCalendar()
           
static java.util.Date getSystemDate()
           
 java.util.TimeZone getTimeZone()
           
 java.util.Calendar toCalendar(java.lang.Object obj)
          Converts an object to an instance of Calendar using the locale returned by getLocale() if necessary.
static java.util.Calendar toCalendar(java.lang.Object obj, java.util.Locale locale)
          Converts an object to an instance of Calendar using the locale returned by getLocale() if necessary.
static java.util.Date toDate(java.lang.Object obj)
          Converts an object to an instance of Date.
 java.util.Date toDate(java.lang.String format, java.lang.Object obj)
          Converts an object to an instance of Date using the specified format and the Locale returned by getLocale() if the object is not already an instance of Date or Calendar.
static java.util.Date toDate(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
          Converts an object to an instance of Date using the specified format and Localeif the object is not already an instance of Date or Calendar.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DateTool

public DateTool()
Default constructor.

Method Detail

getSystemDate

public static final java.util.Date getSystemDate()
Returns:
the system's current time as a Date

getSystemCalendar

public static final java.util.Calendar getSystemCalendar()
Returns:
the system's current time as a Calendar

getLocale

public java.util.Locale getLocale()
Returns:
the default Locale

getTimeZone

public java.util.TimeZone getTimeZone()
Returns:
the default TimeZone

getDate

public java.util.Date getDate()
Returns a Date derived from the result of getCalendar()

Returns:
a Date derived from the result of getCalendar()

getCalendar

public java.util.Calendar getCalendar()
Returns a Calendar instance created using the timezone and locale returned by getTimeZone() and getLocale(). This allows subclasses to easily override the default locale and timezone used by this tool. Sub-classes may override this method to return a Calendar instance not based on the system date. Doing so will also cause the getDate(), getNormalDate(), getFormattedDate(), and toString() methods to return dates equivalent to the Calendar returned by this method, because those methods return values derived from the result of this method.

Returns:
a Calendar instance created using the results of getTimeZone() and getLocale().
See Also:
Calendar.getInstance(TimeZone zone, Locale aLocale)

getFormattedDate

public java.lang.String getFormattedDate(java.lang.String format)
Returns a formatted string representing the date as returned by getDate(). This method uses the same formatting instructions as SimpleDateFormat:

Parameters:
format - the formatting instructions
Returns:
a formatted string representing the date returned by getDate() or null if the parameters are invalid
See Also:
format(String format, Object obj, Locale locale)

format

public java.lang.String format(java.lang.String format,
                               java.lang.Object obj)
Converts the specified object to a date and returns a formatted string representing that date using the locale returned by getLocale(). This method uses the same formatting instructions as SimpleDateFormat:

Parameters:
format - the formatting instructions
obj - the date object to be formatted
Returns:
a formatted string for this locale representing the specified date or null if the parameters are invalid
See Also:
format(String format, Object obj, Locale locale)

format

public static java.lang.String format(java.lang.String format,
                                      java.lang.Object obj,
                                      java.util.Locale locale)
Returns a formatted string representing the specified date and locale.

This method uses the same formatting instructions as SimpleDateFormat:

   Symbol   Meaning                 Presentation        Example
   ------   -------                 ------------        -------
   G        era designator          (Text)              AD
   y        year                    (Number)            1996
   M        month in year           (Text & Number)     July & 07
   d        day in month            (Number)            10
   h        hour in am/pm (1~12)    (Number)            12
   H        hour in day (0~23)      (Number)            0
   m        minute in hour          (Number)            30
   s        second in minute        (Number)            55
   S        millisecond             (Number)            978
   E        day in week             (Text)              Tuesday
   D        day in year             (Number)            189
   F        day of week in month    (Number)            2 (2nd Wed in July)
   w        week in year            (Number)            27
   W        week in month           (Number)            2
   a        am/pm marker            (Text)              PM
   k        hour in day (1~24)      (Number)            24
   K        hour in am/pm (0~11)    (Number)            0
   z        time zone               (Text)              Pacific Standard Time
   '        escape for text         (Delimiter)
   ''       single quote            (Literal)           '

   Examples: "E, MMMM d" will result in "Tue, July 24"
             "EEE, M-d (H:m)" will result in "Tuesday, 7-24 (14:12)"
 

Parameters:
format - the formatting instructions
obj - the date to format
locale - the Locale to format the date for
Returns:
a formatted string representing the specified date or null if the parameters are invalid

toDate

public static java.util.Date toDate(java.lang.Object obj)
Converts an object to an instance of Date. Uses a DateFormat to parse the string value of the object if it is not an instance of Date or Calendar or Long.

Parameters:
obj - the date to convert
Returns:
the object as a Date or null if no conversion is possible

toDate

public java.util.Date toDate(java.lang.String format,
                             java.lang.Object obj)
Converts an object to an instance of Date using the specified format and the Locale returned by getLocale() if the object is not already an instance of Date or Calendar.

Parameters:
format - - the format the date is in
obj - - the date to convert
Returns:
the object as a Date or null if no conversion is possible
See Also:
toDate(String format, Object obj, Locale locale)

toDate

public static java.util.Date toDate(java.lang.String format,
                                    java.lang.Object obj,
                                    java.util.Locale locale)
Converts an object to an instance of Date using the specified format and Localeif the object is not already an instance of Date or Calendar.

Parameters:
format - - the format the date is in
obj - - the date to convert
locale - - the Locale
Returns:
the object as a Date or null if no conversion is possible
See Also:
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)

toCalendar

public java.util.Calendar toCalendar(java.lang.Object obj)
Converts an object to an instance of Calendar using the locale returned by getLocale() if necessary.

Parameters:
obj - the date to convert
Returns:
the converted date
See Also:
toCalendar(Object obj, Locale locale)

toCalendar

public static java.util.Calendar toCalendar(java.lang.Object obj,
                                            java.util.Locale locale)
Converts an object to an instance of Calendar using the locale returned by getLocale() if necessary.

Parameters:
obj - the date to convert
locale - the locale used
Returns:
the converted date
See Also:
toDate(String format, Object obj, Locale locale), Calendar

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
the result of getDate() as a string


Copyright (c) 2003 Apache Software Foundation