类 DateTool
- 直接已知子类:
ComparisonDateTool
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 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 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>
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.
- 从以下版本开始:
- VelocityTools 1.0
- 版本:
- $Revision: 719489 $ $Date: 2008-11-20 22:05:11 -0800 (Thu, 20 Nov 2008) $
- 作者:
- Nathan Bubna
-
字段概要
字段修饰符和类型字段说明static final String
已过时。static final String
已过时。private TimeZone
static final String
The key used for specifying a default timezone via tool configuration.从类继承的字段 org.apache.velocity.tools.generic.FormatConfig
DEFAULT_FORMAT, FORMAT_KEY
从类继承的字段 org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE
从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected void
configure
(ValueParser values) Does the actual configuration.Converts the specified object to a date and formats it according to the pattern or style returned byFormatConfig.getFormat()
.Converts the specified object to a date and returns a formatted string representing that date in the locale returned byLocaleConfig.getLocale()
.Converts the specified object to a date and returns a formatted string representing that date in the specifiedLocale
.Returns the specified date as a string formatted according to the specified date and/or time styles.Returns the specified date as a string formatted according to the specifiedLocale
and date and/or time styles.Returns the specified date as a string formatted according to the specifiedLocale
and date and/or time styles.Returns a formatted string representing the date returned bygetDate()
.Returns a formatted string representing the date and/or time given bygetDate()
in standard, localized patterns.Returns aCalendar
instance created using the timezone and locale returned by getTimeZone() and getLocale().getDate()
Returns aDate
derived from the result ofgetCalendar()
protected DateFormat
getDateFormat
(int dateStyle, int timeStyle, Locale locale, TimeZone timezone) 已过时。getDateFormat
(String dateStyle, String timeStyle, Locale locale, TimeZone timezone) getDateFormat
(String format, Locale locale, TimeZone timezone) getDay()
Returns the day (of the month) value of the date returned bygetCalendar()
.Returns the day (of the month) value for the specified date.getMonth()
Returns the month value of the date returned bygetCalendar()
.Returns the month value of the specified date.protected int
getStyleAsInt
(String style) 已过时。static final Calendar
static final Date
static final long
Returns the configuredTimeZone
.Returns the specified value of the specified date, or null if the field or date is invalid.Return the specified value of the date returned bygetCalendar()
or null if the field is invalid.Returns the specified value of the specified date, or null if the field or date is invalid.getYear()
Returns the year value of the date returned bygetCalendar()
.Returns the year value of the specified date.protected void
setTimeZone
(TimeZone timezone) toCalendar
(Object obj) Converts an object to an instance ofCalendar
using the locale returned byLocaleConfig.getLocale()
if necessary.toCalendar
(Object obj, Locale locale) Converts an object to an instance ofCalendar
using the locale returned byLocaleConfig.getLocale()
if necessary.Converts an object to an instance ofDate
using the format returned byFormatConfig.getFormat()
,theLocale
returned byLocaleConfig.getLocale()
, and theTimeZone
returned bygetTimeZone()
if the object is not already an instance of Date, Calendar, or Long.Converts an object to an instance ofDate
using the specified format,theLocale
returned byLocaleConfig.getLocale()
, and theTimeZone
returned bygetTimeZone()
if the object is not already an instance of Date, Calendar, or Long.toString()
从类继承的方法 org.apache.velocity.tools.generic.FormatConfig
getFormat, setFormat
从类继承的方法 org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale
从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
构造器详细资料
-
DateTool
public DateTool()
-
-
方法详细资料
-
configure
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- 覆盖:
configure
在类中FormatConfig
-
setTimeZone
-
getSystemTime
public static final long getSystemTime()- 返回:
- the system's current time as the number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT.
-
getSystemDate
- 返回:
- the system's current time as a
Date
-
getSystemCalendar
- 返回:
- the system's current time as a
Calendar
-
getTimeZone
Returns the configuredTimeZone
. Default value is fromTimeZone.getDefault()
.- 返回:
- the configured
TimeZone
-
getDate
Returns aDate
derived from the result ofgetCalendar()
- 返回:
- a
Date
derived from the result ofgetCalendar()
-
getCalendar
Returns aCalendar
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.
- 返回:
- a
Calendar
instance created using the results ofgetTimeZone()
andLocaleConfig.getLocale()
. - 另请参阅:
-
getYear
Returns the year value of the date returned bygetCalendar()
.- 从以下版本开始:
- VelocityTools 1.2
-
getYear
Returns the year value of the specified date.- 从以下版本开始:
- VelocityTools 1.2
-
getMonth
Returns the month value of the date returned bygetCalendar()
.- 从以下版本开始:
- VelocityTools 1.2
-
getMonth
Returns the month value of the specified date.- 从以下版本开始:
- VelocityTools 1.2
-
getDay
Returns the day (of the month) value of the date returned bygetCalendar()
.
NOTE: Unlike java.util.Date, this returns the day of the month. It is equivalent to Date.getDate() and Calendar.get(Calendar.DAY_OF_MONTH). We could not call this method getDate() because that already exists in this class with a different function.- 从以下版本开始:
- VelocityTools 1.2
-
getDay
Returns the day (of the month) value for the specified date.
NOTE: Unlike java.util.Date, this returns the day of the month. It is equivalent to Date.getDate() and Calendar.get(Calendar.DAY_OF_MONTH). We could not call this method getDate() because that already exists in this class with a different function.- 从以下版本开始:
- VelocityTools 1.2
-
getValue
Return the specified value of the date returned bygetCalendar()
or null if the field is invalid.- 从以下版本开始:
- VelocityTools 1.2
-
getValue
Returns the specified value of the specified date, or null if the field or date is invalid. The field may be an Integer or it may be the name of the field as a String.- 参数:
field
- the corresponding Integer value or String name of the desired valuedate
- the date/calendar from which the field value will be taken- 从以下版本开始:
- VelocityTools 1.2
-
getValue
Returns the specified value of the specified date, or null if the field or date is invalid.- 参数:
field
- the int for the desired field (e.g. Calendar.MONTH)date
- the date/calendar from which the field value will be taken- 从以下版本开始:
- VelocityTools 1.2
-
get
Returns a formatted string representing the date returned bygetDate()
. 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- 返回:
- a formatted representation of the date returned by
getDate()
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
get
Returns a formatted string representing the date and/or time given bygetDate()
in standard, localized patterns.- 参数:
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the time- 返回:
- a formatted representation of the date returned by
getDate()
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
format
Converts the specified object to a date and formats it according to the pattern or style returned byFormatConfig.getFormat()
.- 参数:
obj
- the date object to be formatted- 返回:
- the specified date formatted as a string
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
format
Converts the specified object to a date and returns a formatted string representing that date in the locale returned byLocaleConfig.getLocale()
.- 参数:
format
- the formatting instructionsobj
- the date object to be formatted- 返回:
- a formatted string for this locale representing the specified
date or
null
if the parameters are invalid - 另请参阅:
-
format
Converts the specified object to a date and returns a formatted string representing that date in the specifiedLocale
.- 参数:
format
- the formatting instructionsobj
- the date object to be formattedlocale
- the locale to be used when formatting- 返回:
- the given date as a formatted string
- 另请参阅:
-
format
Returns a formatted string representing the specified date,Locale
, andTimeZone
.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
Returns the specified date as a string formatted according to the specified date and/or time styles.- 参数:
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formatted- 返回:
- a formatted representation of the given date
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
format
Returns the specified date as a string formatted according to the specifiedLocale
and date and/or time styles.- 参数:
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formattedlocale
- theLocale
to be used for formatting the date- 返回:
- a formatted representation of the given date
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
format
public String format(String dateStyle, String timeStyle, Object obj, Locale locale, TimeZone timezone) Returns the specified date as a string formatted according to the specifiedLocale
and date and/or time styles.- 参数:
dateStyle
- the style pattern for the datetimeStyle
- the style pattern for the timeobj
- the date to be formattedlocale
- theLocale
to be used for formatting the datetimezone
- theTimeZone
the date should be formatted for- 返回:
- a formatted representation of the given date
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
getDateFormat
Returns aDateFormat
instance for the specified format,Locale
, andTimeZone
. 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 customizedSimpleDateFormat
will be returned.- 参数:
format
- the custom or standard formatting pattern to be usedlocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
getDateFormat
public DateFormat getDateFormat(String dateStyle, String timeStyle, Locale locale, TimeZone timezone) - 参数:
dateStyle
- the date styletimeStyle
- the time stylelocale
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
getDateFormat
@Deprecated protected DateFormat getDateFormat(int dateStyle, int timeStyle, Locale locale, TimeZone 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
- theLocale
to be usedtimezone
- theTimeZone
to be used- 返回:
- an instance of
DateFormat
ornull
if an instance cannot be constructed with the given parameters - 从以下版本开始:
- VelocityTools 1.1
-
getStyleAsInt
已过时。Checks a string to see if it matches one of the standard DateFormat style patterns: FULL, LONG, MEDIUM, SHORT, or DEFAULT. If it does, it will return the integer constant for that pattern. If not, it will return -1.- 参数:
style
- the string to be checked- 返回:
- the int identifying the style pattern
- 从以下版本开始:
- VelocityTools 1.1
- 另请参阅:
-
toDate
Converts an object to an instance ofDate
using the format returned byFormatConfig.getFormat()
,theLocale
returned byLocaleConfig.getLocale()
, and theTimeZone
returned bygetTimeZone()
if the object is not already an instance of Date, Calendar, or Long.- 参数:
obj
- the date to convert- 返回:
- the object as a
Date
ornull
if no conversion is possible
-
toDate
Converts an object to an instance ofDate
using the specified format,theLocale
returned byLocaleConfig.getLocale()
, and theTimeZone
returned bygetTimeZone()
if the object is not already an instance of Date, Calendar, or Long.- 参数:
format
- - the format the date is inobj
- - the date to convert- 返回:
- the object as a
Date
ornull
if no conversion is possible - 另请参阅:
-
toDate
Converts an object to an instance ofDate
using the specified format andLocale
if the object is not already an instance of Date, Calendar, or Long. -
toDate
Converts an object to an instance ofDate
using the specified format,Locale
, andTimeZone
if the object is not already an instance of Date, Calendar, or Long. -
toCalendar
Converts an object to an instance ofCalendar
using the locale returned byLocaleConfig.getLocale()
if necessary.- 参数:
obj
- the date to convert- 返回:
- the converted date
- 另请参阅:
-
toCalendar
Converts an object to an instance ofCalendar
using the locale returned byLocaleConfig.getLocale()
if necessary.- 参数:
obj
- the date to convertlocale
- the locale used- 返回:
- the converted date
- 另请参阅:
-
toString
- 覆盖:
toString
在类中Object
- 返回:
- the result of
getDate()
formatted according to the result ofFormatConfig.getFormat()
. - 另请参阅:
-