org.apache.struts2.components
Class Date

java.lang.Object
  extended by org.apache.struts2.components.Component
      extended by org.apache.struts2.components.Date

public class Date
extends Component

Format Date object in different ways.

The date tag will allow you to format a Date in a quick and easy way. You can specify a custom format (eg. "dd/MM/yyyy hh:mm"), you can generate easy readable notations (like "in 2 hours, 14 minutes"), or you can just fall back on a predefined format with key 'struts.date.format' in your properties file. If that key is not defined, it will finally fall back to the default DateFormat.MEDIUM formatting. Note: If the requested Date object isn't found on the stack, a blank will be returned.

Configurable attributes are :-

Following how the date component will work, depending on the value of nice attribute (which by default is false) and the format attribute.

Condition 1: With nice attribute as true

i18n key default
struts.date.format.past {0} ago
struts.date.format.future in {0}
struts.date.format.seconds an instant
struts.date.format.minutes {0,choice,1#one minute|1<{0} minutes}
struts.date.format.hours {0,choice,1#one hour|1<{0} hours}{1,choice,0#|1#, one minute|1<, {1} minutes}
struts.date.format.days {0,choice,1#one day|1<{0} days}{1,choice,0#|1#, one hour|1<, {1} hours}
struts.date.format.years {0,choice,1#one year|1<{0} years}{1,choice,0#|1#, one day|1<, {1} days}

Condition 2: With nice attribute as false and format attribute is specified eg. dd/MM/yyyyy

In this case the format attribute will be used.

Condition 3: With nice attribute as false and no format attribute is specified

i18n key default
struts.date.format if one is not found DateFormat.MEDIUM format will be used

Examples

  
  <s:date name="person.birthday" format="dd/MM/yyyy" />
  <s:date name="person.birthday" format="%{getText('some.i18n.key')}" />
  <s:date name="person.birthday" nice="true" />
  <s:date name="person.birthday" />
  
 
Date


Field Summary
static java.lang.String DATETAG_PROPERTY
          Property name to fall back when no format is specified
static java.lang.String DATETAG_PROPERTY_DAYS
          Property name that defines the days notation (default: {0,choice,1#one day|1<{0} days}{1,choice,0#|1#, one hour|1<, {1} hours})
static java.lang.String DATETAG_PROPERTY_FUTURE
          Property name that defines the future notation (default: in {0})
static java.lang.String DATETAG_PROPERTY_HOURS
          Property name that defines the hours notation (default: {0,choice,1#one hour|1<{0} hours}{1,choice,0#|1#, one minute|1<, {1} minutes})
static java.lang.String DATETAG_PROPERTY_MINUTES
          Property name that defines the minutes notation (default: {0,choice,1#one minute|1<{0} minutes})
static java.lang.String DATETAG_PROPERTY_PAST
          Property name that defines the past notation (default: {0} ago)
static java.lang.String DATETAG_PROPERTY_SECONDS
          Property name that defines the seconds notation (default: in instant)
static java.lang.String DATETAG_PROPERTY_YEARS
          Property name that defines the years notation (default: {0,choice,1#one year|1<{0} years}{1,choice,0#|1#, one day|1<, {1} days})
 
Fields inherited from class org.apache.struts2.components.Component
COMPONENT_STACK, id, parameters, stack
 
Constructor Summary
Date(com.opensymphony.xwork2.util.OgnlValueStack stack)
           
 
Method Summary
 boolean end(java.io.Writer writer, java.lang.String body)
          Callback for the end tag of this component.
 java.lang.String formatTime(com.opensymphony.xwork2.TextProvider tp, java.util.Date date)
          Calculates the difference in time from now to the given date, and outputs it nicely.
 java.lang.String getFormat()
           
 java.lang.String getName()
           
 boolean isNice()
           
 void setFormat(java.lang.String format)
          Date or DateTime format pattern
 void setName(java.lang.String name)
          The date value to format
 void setNice(boolean nice)
          Whether to print out the date nicely
 
Methods inherited from class org.apache.struts2.components.Component
addAllParameters, addParameter, altSyntax, copyParams, determineActionURL, determineNamespace, end, fieldError, findAncestor, findString, findString, findValue, findValue, findValue, getComponentStack, getId, getParameters, getStack, popComponentStack, setId, start, toString, usesBody
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATETAG_PROPERTY

public static final java.lang.String DATETAG_PROPERTY
Property name to fall back when no format is specified

See Also:
Constant Field Values

DATETAG_PROPERTY_PAST

public static final java.lang.String DATETAG_PROPERTY_PAST
Property name that defines the past notation (default: {0} ago)

See Also:
Constant Field Values

DATETAG_PROPERTY_FUTURE

public static final java.lang.String DATETAG_PROPERTY_FUTURE
Property name that defines the future notation (default: in {0})

See Also:
Constant Field Values

DATETAG_PROPERTY_SECONDS

public static final java.lang.String DATETAG_PROPERTY_SECONDS
Property name that defines the seconds notation (default: in instant)

See Also:
Constant Field Values

DATETAG_PROPERTY_MINUTES

public static final java.lang.String DATETAG_PROPERTY_MINUTES
Property name that defines the minutes notation (default: {0,choice,1#one minute|1<{0} minutes})

See Also:
Constant Field Values

DATETAG_PROPERTY_HOURS

public static final java.lang.String DATETAG_PROPERTY_HOURS
Property name that defines the hours notation (default: {0,choice,1#one hour|1<{0} hours}{1,choice,0#|1#, one minute|1<, {1} minutes})

See Also:
Constant Field Values

DATETAG_PROPERTY_DAYS

public static final java.lang.String DATETAG_PROPERTY_DAYS
Property name that defines the days notation (default: {0,choice,1#one day|1<{0} days}{1,choice,0#|1#, one hour|1<, {1} hours})

See Also:
Constant Field Values

DATETAG_PROPERTY_YEARS

public static final java.lang.String DATETAG_PROPERTY_YEARS
Property name that defines the years notation (default: {0,choice,1#one year|1<{0} years}{1,choice,0#|1#, one day|1<, {1} days})

See Also:
Constant Field Values
Constructor Detail

Date

public Date(com.opensymphony.xwork2.util.OgnlValueStack stack)
Method Detail

formatTime

public java.lang.String formatTime(com.opensymphony.xwork2.TextProvider tp,
                                   java.util.Date date)
Calculates the difference in time from now to the given date, and outputs it nicely.

An example:
Now = 2006/03/12 13:38:00, date = 2006/03/12 15:50:00 will output "in 1 hour, 12 minutes".

Parameters:
tp - text provider
date - the date
Returns:
the date nicely

end

public boolean end(java.io.Writer writer,
                   java.lang.String body)
Description copied from class: Component
Callback for the end tag of this component. Should the body be evaluated again?

NOTE: will pop component stack.

Overrides:
end in class Component
Parameters:
writer - the output writer.
body - the rendered body.
Returns:
true if the body should be evaluated again

setFormat

public void setFormat(java.lang.String format)
Date or DateTime format pattern


setNice

public void setNice(boolean nice)
Whether to print out the date nicely


getName

public java.lang.String getName()
Returns:
Returns the name.

setName

public void setName(java.lang.String name)
The date value to format


getFormat

public java.lang.String getFormat()
Returns:
Returns the format.

isNice

public boolean isNice()
Returns:
Returns the nice.


Copyright © 2000-2006 Apache Software Foundation. All Rights Reserved.