|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.velocity.tools.generic.DateTool
Tool for working with Date
and Calendar
in Velocity templates. It is useful for accessing and
formatting the "current" date as well as for formatting
arbitrary Date
and Calendar
objects. Also
the tool can be used to retrieve DateFormat
instances
or make conversions to and from various date types.
Example uses: $date -> Oct 19, 2003 9:54:50 PM $date.long -> October 19, 2003 9:54:50 PM PDT $date.medium_time -> 9:54:50 PM $date.full_date -> Sunday, October 19, 2003 $date.get('default','short') -> Oct 19, 2003 9:54 PM $date.get('yyyy-M-d H:m:s') -> 2003-10-19 21:54:50 $myDate -> Tue Oct 07 03:14:50 PDT 2003 $date.format('medium',$myDate) -> Oct 7, 2003 3:14:50 AM Example toolbox.xml config (if you want to use this with VelocityView): <tool> <key>date</key> <scope>application</scope> <class>org.apache.velocity.tools.generic.DateTool</class> </tool>
This tool is entirely threadsafe, and has no instance members. It may be used in any scope (request, session, or application). As such, the methods are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default format, calendar, locale, or timezone.
Field Summary | |
static java.lang.String |
DEFAULT_FORMAT
The default format to be used when none is specified. |
Constructor Summary | |
DateTool()
Default constructor. |
Method Summary | |
java.lang.String |
format(java.lang.Object obj)
Converts the specified object to a date and formats it according to the pattern or style returned by getFormat() . |
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 in the locale returned by getLocale() . |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale)
Converts the specified object to a date and returns a formatted string representing that date in the specified Locale . |
java.lang.String |
format(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale,
java.util.TimeZone timezone)
Returns a formatted string representing the specified date, Locale , and TimeZone . |
java.lang.String |
format(java.lang.String dateStyle,
java.lang.String timeStyle,
java.lang.Object obj)
Returns the specified date as a string formatted according to the specified date and/or time styles. |
java.lang.String |
format(java.lang.String dateStyle,
java.lang.String timeStyle,
java.lang.Object obj,
java.util.Locale locale)
Returns the specified date as a string formatted according to the specified Locale and date and/or time styles. |
java.lang.String |
format(java.lang.String dateStyle,
java.lang.String timeStyle,
java.lang.Object obj,
java.util.Locale locale,
java.util.TimeZone timezone)
Returns the specified date as a string formatted according to the specified Locale and date and/or time styles. |
java.lang.String |
get(java.lang.String format)
Returns a formatted string representing the date returned by getDate() . |
java.lang.String |
get(java.lang.String dateStyle,
java.lang.String timeStyle)
Returns a formatted string representing the date and/or time given by getDate() in standard, localized patterns. |
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() |
protected java.text.DateFormat |
getDateFormat(int dateStyle,
int timeStyle,
java.util.Locale locale,
java.util.TimeZone timezone)
Returns a DateFormat instance for the specified
time style, date style, Locale , and TimeZone . |
java.text.DateFormat |
getDateFormat(java.lang.String format,
java.util.Locale locale,
java.util.TimeZone timezone)
Returns a DateFormat instance for the specified
format, Locale , and TimeZone . |
java.text.DateFormat |
getDateFormat(java.lang.String dateStyle,
java.lang.String timeStyle,
java.util.Locale locale,
java.util.TimeZone timezone)
Returns a DateFormat instance for the specified
date style, time style, Locale , and TimeZone . |
java.lang.Integer |
getDay()
Returns the day (of the month) value of the date returned by getCalendar() . |
java.lang.Integer |
getDay(java.lang.Object date)
Returns the day (of the month) value for the specified date. |
java.lang.String |
getFormat()
Return the pattern or style to be used for formatting dates when none is specified. |
java.util.Locale |
getLocale()
This implementation returns the default locale. |
java.lang.Integer |
getMonth()
Returns the month value of the date returned by getCalendar() . |
java.lang.Integer |
getMonth(java.lang.Object date)
Returns the month value of the specified date. |
protected int |
getStyleAsInt(java.lang.String style)
Checks a string to see if it matches one of the standard DateFormat style patterns: FULL, LONG, MEDIUM, SHORT, or DEFAULT. |
static java.util.Calendar |
getSystemCalendar()
|
static java.util.Date |
getSystemDate()
|
java.util.TimeZone |
getTimeZone()
This implementation returns the default TimeZone. |
java.lang.Integer |
getValue(int field,
java.lang.Object date)
Returns the specified value of the specified date, or null if the field or date is invalid. |
java.lang.Integer |
getValue(java.lang.Object field)
Return the specified value of the date returned by getCalendar() or null if the field is invalid. |
java.lang.Integer |
getValue(java.lang.Object field,
java.lang.Object date)
Returns the specified value of the specified date, or null if the field or date is invalid. |
java.lang.Integer |
getYear()
Returns the year value of the date returned by getCalendar() . |
java.lang.Integer |
getYear(java.lang.Object date)
Returns the year value of the specified date. |
java.util.Calendar |
toCalendar(java.lang.Object obj)
Converts an object to an instance of Calendar using the
locale returned by getLocale() if necessary. |
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. |
java.util.Date |
toDate(java.lang.Object obj)
Converts an object to an instance of Date using the
format returned by getFormat() ,the Locale returned
by getLocale() , and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long. |
java.util.Date |
toDate(java.lang.String format,
java.lang.Object obj)
Converts an object to an instance of Date using the
specified format,the Locale returned by
getLocale() , and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long. |
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 Locale if the object is not already
an instance of Date, Calendar, or Long. |
java.util.Date |
toDate(java.lang.String format,
java.lang.Object obj,
java.util.Locale locale,
java.util.TimeZone timezone)
Converts an object to an instance of Date using the
specified format, Locale , and TimeZone if the
object is not already an instance of Date, Calendar, or Long. |
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String DEFAULT_FORMAT
Constructor Detail |
public DateTool()
Method Detail |
public static final java.util.Date getSystemDate()
Date
public static final java.util.Calendar getSystemCalendar()
Calendar
public java.util.Locale getLocale()
Locale
public java.util.TimeZone getTimeZone()
TimeZone
public java.util.Date getDate()
Date
derived from the result of getCalendar()
Date
derived from the result of getCalendar()
public java.util.Calendar getCalendar()
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(), get(String), get(String,String), 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.
Calendar
instance created using the results of
getTimeZone()
and getLocale()
.Calendar.getInstance(TimeZone zone, Locale aLocale)
public java.lang.String getFormat()
NOTE: At some point in the future it may be feasible to configure this value via the toolbox definition, but at present, it is not possible to specify custom tool configurations there. For now you should just override this in a subclass to have a different default.
public java.lang.Integer getYear()
getCalendar()
.
public java.lang.Integer getYear(java.lang.Object date)
public java.lang.Integer getMonth()
getCalendar()
.
public java.lang.Integer getMonth(java.lang.Object date)
public java.lang.Integer getDay()
getCalendar()
.
public java.lang.Integer getDay(java.lang.Object date)
public java.lang.Integer getValue(java.lang.Object field)
getCalendar()
or null if the field is invalid.
public java.lang.Integer getValue(java.lang.Object field, java.lang.Object date)
field
- the corresponding Integer value or String name of the desired valuedate
- the date/calendar from which the field value will be takenpublic java.lang.Integer getValue(int field, java.lang.Object date)
field
- the int for the desired field (e.g. Calendar.MONTH)date
- the date/calendar from which the field value will be takenpublic java.lang.String get(java.lang.String format)
getDate()
. In its default implementation, this method
allows you to retrieve the current date in standard formats by
simply doing things like $date.medium
or
$date.full
. If you want only the date or time portion
you can specify that along with the standard formats. (e.g.
$date.medium_date
or $date.short_time
)
More complex or custom formats can be retrieved
by using the full method syntax. (e.g. $date.get('E, MMMM d'))
format
- the formatting instructions
getDate()
format(String format, Object obj, Locale locale, TimeZone timezone)
public java.lang.String get(java.lang.String dateStyle, java.lang.String timeStyle)
getDate()
in standard, localized patterns.
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the time
getDate()
DateFormat
,
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.Object obj)
getFormat()
.
obj
- the date object to be formatted
format(String format, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.String format, java.lang.Object obj)
getLocale()
.
format
- the formatting instructionsobj
- the date object to be formatted
null
if the parameters are invalidformat(String format, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
Locale
.
format
- the formatting instructionsobj
- the date object to be formattedlocale
- the locale to be used when formatting
format(String format, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.String format, java.lang.Object obj, java.util.Locale locale, java.util.TimeZone timezone)
Locale
, and TimeZone
.
The specified format may be a standard style pattern ('full', 'long', 'medium', 'short', or 'default').
You may also specify that you want only the date or time portion be appending '_date' or '_time' respectively to the standard style pattern. (e.g. 'full_date' or 'long_time')
If the format fits neither of these patterns, then the output
will be formatted according to the symbols defined by
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)"
format
- the custom or standard pattern to be usedobj
- the date to formatlocale
- the Locale
to format the date fortimezone
- the TimeZone
to be used when formatting
null
if the parameters are invalidpublic java.lang.String format(java.lang.String dateStyle, java.lang.String timeStyle, java.lang.Object obj)
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formatted
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.String dateStyle, java.lang.String timeStyle, java.lang.Object obj, java.util.Locale locale)
Locale
and date and/or time styles.
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formattedlocale
- the Locale
to be used for formatting the date
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
public java.lang.String format(java.lang.String dateStyle, java.lang.String timeStyle, java.lang.Object obj, java.util.Locale locale, java.util.TimeZone timezone)
Locale
and date and/or time styles.
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formattedlocale
- the Locale
to be used for formatting the datetimezone
- the TimeZone
the date should be formatted for
DateFormat
,
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)
public java.text.DateFormat getDateFormat(java.lang.String format, java.util.Locale locale, java.util.TimeZone timezone)
DateFormat
instance for the specified
format, Locale
, and TimeZone
. If the format
specified is a standard style pattern, then a date-time instance
will be returned with both the date and time styles set to the
specified style. If it is a custom format, then a customized
SimpleDateFormat
will be returned.
format
- the custom or standard formatting pattern to be usedlocale
- the Locale
to be usedtimezone
- the TimeZone
to be used
DateFormat
SimpleDateFormat
,
DateFormat
public java.text.DateFormat getDateFormat(java.lang.String dateStyle, java.lang.String timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
DateFormat
instance for the specified
date style, time style, Locale
, and TimeZone
.
dateStyle
- the date styletimeStyle
- the time stylelocale
- the Locale
to be usedtimezone
- the TimeZone
to be used
DateFormat
getDateFormat(int timeStyle, int dateStyle, Locale locale, TimeZone timezone)
protected java.text.DateFormat getDateFormat(int dateStyle, int timeStyle, java.util.Locale locale, java.util.TimeZone timezone)
DateFormat
instance for the specified
time style, date style, Locale
, and TimeZone
.
dateStyle
- the date style (date will be ignored if this is
less than zero and the date style is not)timeStyle
- the time style (time will be ignored if this is
less than zero and the date style is not)locale
- the Locale
to be usedtimezone
- the TimeZone
to be used
DateFormat
or null
if an instance cannot be constructed with the given
parametersprotected int getStyleAsInt(java.lang.String style)
style
- the string to be checked
DateFormat
public java.util.Date toDate(java.lang.Object obj)
Date
using the
format returned by getFormat()
,the Locale
returned
by getLocale()
, and the TimeZone
returned by
getTimeZone()
if the object is not already an instance
of Date, Calendar, or Long.
obj
- the date to convert
Date
or null
if no
conversion is possiblepublic java.util.Date toDate(java.lang.String format, java.lang.Object obj)
Date
using the
specified format,the Locale
returned by
getLocale()
, and the TimeZone
returned by
getTimeZone()
if the object is not already an instance
of Date, Calendar, or Long.
format
- - the format the date is inobj
- - the date to convert
Date
or null
if no
conversion is possibletoDate(String format, Object obj, Locale locale)
public java.util.Date toDate(java.lang.String format, java.lang.Object obj, java.util.Locale locale)
Date
using the
specified format and Locale
if the object is not already
an instance of Date, Calendar, or Long.
format
- - the format the date is inobj
- - the date to convertlocale
- - the Locale
Date
or null
if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public java.util.Date toDate(java.lang.String format, java.lang.Object obj, java.util.Locale locale, java.util.TimeZone timezone)
Date
using the
specified format, Locale
, and TimeZone
if the
object is not already an instance of Date, Calendar, or Long.
format
- - the format the date is inobj
- - the date to convertlocale
- - the Locale
timezone
- - the TimeZone
Date
or null
if no
conversion is possiblegetDateFormat(java.lang.String, java.util.Locale, java.util.TimeZone)
,
SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)
public java.util.Calendar toCalendar(java.lang.Object obj)
Calendar
using the
locale returned by getLocale()
if necessary.
obj
- the date to convert
toCalendar(Object obj, Locale locale)
public java.util.Calendar toCalendar(java.lang.Object obj, java.util.Locale locale)
Calendar
using the
locale returned by getLocale()
if necessary.
obj
- the date to convertlocale
- the locale used
toDate(String format, Object obj, Locale locale)
,
Calendar
public java.lang.String toString()
getDate()
formatted according to the result
of getFormat()
.format(String format, Object obj)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |