@DefaultKey(value="date") public class DateTool extends FormatConfig implements Serializable
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.
Possible formats include:
DateFormat, optionally suffixed by '_date' or '_time' to get a date-only or time-only formatSimpleDateFormatExample of formatting the "current" date:
$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
$date.iso -> 2003-10-19T21:54:50-07:00
$date.iso_tz_time -> 21:54:50-07:00
$date.intl_tz -> 2003-10-19 21:54:50 CET
Example of formatting an arbitrary date:
$myDate -> Tue Oct 07 03:14:50 PDT 2003
$date.format('medium',$myDate) -> Oct 7, 2003 3:14:50 AM
Example tools.xml config (if you want to use this with VelocityView):
<tools>
<toolbox scope="application">
<tool class="org.apache.velocity.tools.generic.DateTool"
format="yyyy-MM-dd"/>
</toolbox>
</tools>
Should you need to use several formats, you can either use explicit formats by means of the toDate(format, date) method,
or you can declare several date tool instances with different formats.
The methods of this tool are highly interconnected, and overriding key methods provides an easy way to create subclasses that use a non-default format, calendar, locale, or timezone.
| Modifier and Type | Field and Description |
|---|---|
static String |
TIMEZONE_KEY
The key used for specifying a default timezone via tool configuration.
|
DEFAULT_FORMAT, FORMAT_KEYDEFAULT_LOCALELOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY| Constructor and Description |
|---|
DateTool() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
configure(ValueParser values)
Does the actual configuration.
|
String |
format(Object obj)
Converts the specified object to a date and formats it according to
the pattern or style returned by
FormatConfig.getFormat(). |
String |
format(String format,
Object obj)
Converts the specified object to a date and returns
a formatted string representing that date in the locale
returned by
LocaleConfig.getLocale(). |
String |
format(String format,
Object obj,
Locale locale)
Converts the specified object to a date and returns
a formatted string representing that date in the specified
Locale. |
String |
format(String format,
Object obj,
Locale locale,
TimeZone timezone)
|
String |
format(String dateStyle,
String timeStyle,
Object obj)
Returns the specified date as a string formatted according to the
specified date and/or time styles.
|
String |
format(String dateStyle,
String timeStyle,
Object obj,
Locale locale)
Returns the specified date as a string formatted according to the
specified
Locale and date and/or time styles. |
String |
format(String dateStyle,
String timeStyle,
Object obj,
Locale locale,
TimeZone timezone)
Returns the specified date as a string formatted according to the
specified
Locale and date and/or time styles. |
String |
get(String format)
Returns a formatted string representing the date returned by
getDate(). |
String |
get(String dateStyle,
String timeStyle)
Returns a formatted string representing the date and/or time given by
getDate() in standard, localized patterns. |
Calendar |
getCalendar()
Returns a
Calendar instance created using the timezone and
locale returned by getTimeZone() and getLocale(). |
Date |
getDate()
Returns a
Date derived from the result of getCalendar() |
DateFormat |
getDateFormat(String format,
Locale locale,
TimeZone timezone)
|
DateFormat |
getDateFormat(String dateStyle,
String timeStyle,
Locale locale,
TimeZone timezone)
|
Integer |
getDay() |
Integer |
getDay(Object date) |
Integer |
getMonth() |
Integer |
getMonth(Object date) |
static Calendar |
getSystemCalendar() |
static Date |
getSystemDate() |
static long |
getSystemTime() |
TimeZone |
getTimeZone()
Returns the configured
TimeZone. |
Integer |
getValue(int field,
Object date) |
Integer |
getValue(Object field) |
Integer |
getValue(Object field,
Object date) |
Integer |
getYear() |
Integer |
getYear(Object date) |
protected void |
setTimeZone(TimeZone timezone)
Sets time zone
|
Calendar |
toCalendar(Object obj)
Converts an object to an instance of
Calendar using the
format returned by FormatConfig.getFormat(), the
locale returned by LocaleConfig.getLocale() and the time zone
returned by getTimeZone(). |
Calendar |
toCalendar(Object obj,
Locale locale)
Converts an object to an instance of
Calendar using the
provided locale, the format returned by FormatConfig.getFormat() and
the time zone returned by getTimeZone(). |
Calendar |
toCalendar(String format,
Object obj)
Converts an object to an instance of
Calendar using the
provided format, the locale returned by LocaleConfig.getLocale() and
the time zone returned by getTimeZone(). |
Calendar |
toCalendar(String format,
Object obj,
Locale locale)
Converts an object to an instance of
Calendar using the
provided format and locale, and the time zone returned
by getTimeZone(). |
Calendar |
toCalendar(String format,
Object obj,
Locale locale,
TimeZone timezone)
Converts an object to an instance of
Calendar using the
provided format, locale and time zone. |
Date |
toDate(Object obj)
Converts an object to an instance of
Date using the
format returned by FormatConfig.getFormat(),the Locale returned
by LocaleConfig.getLocale(), and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long. |
Date |
toDate(Object obj,
Locale locale)
Converts an object to an instance of
Date using the
format returned by FormatConfig.getFormat(),the provided Locale,
and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long. |
Date |
toDate(Object obj,
Locale locale,
TimeZone timezone)
Converts an object to an instance of
Date using the
format returned by FormatConfig.getFormat(),the provided Locale,
and TimeZone if the object is not already an instance
of Date, Calendar, or Long. |
Date |
toDate(String format,
Object obj)
Converts an object to an instance of
Date using the
specified format,the Locale returned by
LocaleConfig.getLocale(), and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long. |
Date |
toDate(String format,
Object obj,
Locale locale)
|
Date |
toDate(String format,
Object obj,
Locale locale,
TimeZone timezone)
|
String |
toLocalizedPattern(String format,
Locale locale)
Returns a localized date format pattern for the given format.
|
String |
toString() |
getFormat, setFormatgetLocale, setLocale, toLocaleconfigure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeModepublic static final String TIMEZONE_KEY
protected void configure(ValueParser values)
configure in class FormatConfigvalues - configuration valuesprotected void setTimeZone(TimeZone timezone)
timezone - time zonepublic static final long getSystemTime()
public static final Calendar getSystemCalendar()
Calendarpublic TimeZone getTimeZone()
TimeZone. Default value is
from TimeZone.getDefault().TimeZonepublic Date getDate()
Date derived from the result of getCalendar()Date derived from the result of getCalendar()public 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 LocaleConfig.getLocale().Calendar.getInstance(TimeZone zone, Locale aLocale)public Integer getYear()
getCalendar().public Integer getYear(Object date)
date - target datepublic Integer getMonth()
getCalendar().public Integer getMonth(Object date)
date - target datepublic Integer getDay()
getCalendar().
public Integer getDay(Object date)
date - target datepublic Integer getValue(Object field)
field - target fieldgetCalendar() or null if the field is invalid.public Integer getValue(Object field, 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 Integer getValue(int field, 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 String get(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 instructionsgetDate()format(String format, Object obj, Locale locale, TimeZone timezone)public String get(String dateStyle, String timeStyle)
getDate() in standard, localized patterns.dateStyle - the style pattern for the datetimeStyle - the style pattern for the timegetDate()DateFormat,
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)public String format(Object obj)
FormatConfig.getFormat().obj - the date object to be formattedformat(String format, Object obj, Locale locale, TimeZone timezone)public String format(String format, Object obj)
LocaleConfig.getLocale().format - the formatting instructionsobj - the date object to be formattednull if the parameters are invalidformat(String format, Object obj, Locale locale, TimeZone timezone)public String format(String format, Object obj, Locale locale)
Locale.format - the formatting instructionsobj - the date object to be formattedlocale - the locale to be used when formattingformat(String format, Object obj, Locale locale, TimeZone timezone)public String format(String format, Object obj, Locale locale, TimeZone timezone)
Locale, and TimeZone.
The specified format may be a standard style pattern ('full', 'long', 'medium', 'short', or 'default') or extended style pattern ('iso', 'iso_tz', 'intl', 'intl_tz').
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', 'long_time', 'intl_date')
If the format fits neither of these patterns, then the output
will be formatted according to the symbols defined by
SimpleDateFormat.
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 formattingnull if the parameters are invalidpublic String format(String dateStyle, String timeStyle, Object obj)
dateStyle - the style pattern for the datetimeStyle - the style pattern for the timeobj - the date to be formattedformat(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)public String format(String dateStyle, String timeStyle, Object obj, 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 dateformat(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)public String format(String dateStyle, String timeStyle, Object obj, Locale locale, 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 forDateFormat,
format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone)public DateFormat getDateFormat(String format, Locale locale, 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 usedDateFormatSimpleDateFormat,
DateFormatpublic DateFormat getDateFormat(String dateStyle, String timeStyle, Locale locale, TimeZone timezone)
dateStyle - the date styletimeStyle - the time stylelocale - the Locale to be usedtimezone - the TimeZone to be usedDateFormatConversionUtils.getDateFormat(int,int,Locale,TimeZone)public Date toDate(Object obj)
Date using the
format returned by FormatConfig.getFormat(),the Locale returned
by LocaleConfig.getLocale(), and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long.obj - the date to convertDate or null if no
conversion is possiblepublic Date toDate(Object obj, Locale locale)
Date using the
format returned by FormatConfig.getFormat(),the provided Locale,
and the TimeZone returned by
getTimeZone() if the object is not already an instance
of Date, Calendar, or Long.public Date toDate(Object obj, Locale locale, TimeZone timezone)
Date using the
format returned by FormatConfig.getFormat(),the provided Locale,
and TimeZone if the object is not already an instance
of Date, Calendar, or Long.public Date toDate(String format, Object obj)
Date using the
specified format,the Locale returned by
LocaleConfig.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 convertDate or null if no
conversion is possibletoDate(String format, Object obj, Locale locale)public Date toDate(String format, Object obj, 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 LocaleDate or null if no
conversion is possibleSimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)public Date toDate(String format, Object obj, Locale locale, 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 Localetimezone - - the TimeZoneDate 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 Calendar toCalendar(Object obj)
Calendar using the
format returned by FormatConfig.getFormat(), the
locale returned by LocaleConfig.getLocale() and the time zone
returned by getTimeZone().obj - the date to converttoCalendar(String format, Object obj, Locale locale, TimeZone timezone)public Calendar toCalendar(Object obj, Locale locale)
Calendar using the
provided locale, the format returned by FormatConfig.getFormat() and
the time zone returned by getTimeZone().obj - the date to convertlocale - the locale usedtoCalendar(String format, Object obj, Locale locale, TimeZone timezone),
Calendarpublic Calendar toCalendar(String format, Object obj)
Calendar using the
provided format, the locale returned by LocaleConfig.getLocale() and
the time zone returned by getTimeZone().format - the format to useobj - the date to converttoCalendar(String format, Object obj, Locale locale, TimeZone timezone),
Calendarpublic Calendar toCalendar(String format, Object obj, Locale locale)
Calendar using the
provided format and locale, and the time zone returned
by getTimeZone().format - the provided formatobj - the date to convertlocale - the locale usedtoDate(String format, Object obj, Locale locale),
Calendarpublic Calendar toCalendar(String format, Object obj, Locale locale, TimeZone timezone)
Calendar using the
provided format, locale and time zone.format - the provided formatobj - the date to convertlocale - the locale usedtimezone - the time zone usedtoDate(String format, Object obj, Locale locale),
Calendarpublic String toLocalizedPattern(String format, Locale locale)
SimpleDateFormat.toLocalizedPattern()
provides this functionality, and this method merely calls
that on an appropriately-constructed SimpleDateFormat object.format - the custom or standard pattern to convertlocale - the Locale to format for pattern forpublic String toString()
toString in class ObjectgetDate() formatted according to the result
of FormatConfig.getFormat().format(String format, Object obj)Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.