Datepublic 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 |
Fields Summary |
---|
private static final Log | LOG | public static final String | DATETAG_PROPERTYProperty name to fall back when no format is specified | public static final String | DATETAG_PROPERTY_PASTProperty name that defines the past notation (default: {0} ago) | private static final String | DATETAG_DEFAULT_PAST | public static final String | DATETAG_PROPERTY_FUTUREProperty name that defines the future notation (default: in {0}) | private static final String | DATETAG_DEFAULT_FUTURE | public static final String | DATETAG_PROPERTY_SECONDSProperty name that defines the seconds notation (default: in instant) | private static final String | DATETAG_DEFAULT_SECONDS | public static final String | DATETAG_PROPERTY_MINUTESProperty name that defines the minutes notation (default: {0,choice,1#one minute|1<{0} minutes}) | private static final String | DATETAG_DEFAULT_MINUTES | public static final String | DATETAG_PROPERTY_HOURSProperty name that defines the hours notation (default: {0,choice,1#one hour|1<{0} hours}{1,choice,0#|1#, one
minute|1<, {1} minutes}) | private static final String | DATETAG_DEFAULT_HOURS | public static final String | DATETAG_PROPERTY_DAYSProperty name that defines the days notation (default: {0,choice,1#one day|1<{0} days}{1,choice,0#|1#, one hour|1<,
{1} hours}) | private static final String | DATETAG_DEFAULT_DAYS | public static final String | DATETAG_PROPERTY_YEARSProperty name that defines the years notation (default: {0,choice,1#one year|1<{0} years}{1,choice,0#|1#, one
day|1<, {1} days}) | private static final String | DATETAG_DEFAULT_YEARS | private String | name | private String | format | private boolean | nice |
Methods Summary |
---|
public boolean | end(java.io.Writer writer, java.lang.String body)
String msg = null;
ValueStack stack = getStack();
java.util.Date date = null;
// find the name on the valueStack, and cast it to a date
try {
date = (java.util.Date) findValue(name);
} catch (Exception e) {
LOG.error("Could not convert object with key '" + name
+ "' to a java.util.Date instance");
// bad date, return a blank instead ?
msg = "";
}
//try to find the format on the stack
if (format != null) {
format = findString(format);
}
if (date != null) {
TextProvider tp = findProviderInStack();
if (tp != null) {
if (nice) {
msg = formatTime(tp, date);
} else {
if (format == null) {
String globalFormat = null;
// if the format is not specified, fall back using the
// defined property DATETAG_PROPERTY
globalFormat = tp.getText(DATETAG_PROPERTY);
// if tp.getText can not find the property then the
// returned string is the same as input =
// DATETAG_PROPERTY
if (globalFormat != null
&& !DATETAG_PROPERTY.equals(globalFormat)) {
msg = new SimpleDateFormat(globalFormat,
ActionContext.getContext().getLocale())
.format(date);
} else {
msg = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM,
ActionContext.getContext().getLocale())
.format(date);
}
} else {
msg = new SimpleDateFormat(format, ActionContext
.getContext().getLocale()).format(date);
}
}
if (msg != null) {
try {
if (getId() == null) {
writer.write(msg);
} else {
stack.getContext().put(getId(), msg);
}
} catch (IOException e) {
LOG.error("Could not write out Date tag", e);
}
}
}
}
return super.end(writer, "");
| private com.opensymphony.xwork2.TextProvider | findProviderInStack()
for (Iterator iterator = getStack().getRoot().iterator(); iterator
.hasNext();) {
Object o = iterator.next();
if (o instanceof TextProvider) {
return (TextProvider) o;
}
}
return null;
| 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".
java.util.Date now = new java.util.Date();
StringBuffer sb = new StringBuffer();
List args = new ArrayList();
long secs = Math.abs((now.getTime() - date.getTime()) / 1000);
long mins = secs / 60;
long sec = secs % 60;
int min = (int) mins % 60;
long hours = mins / 60;
int hour = (int) hours % 24;
int days = (int) hours / 24;
int day = days % 365;
int years = days / 365;
if (years > 0) {
args.add(new Long(years));
args.add(new Long(day));
args.add(sb);
args.add(null);
sb.append(tp.getText(DATETAG_PROPERTY_YEARS, DATETAG_DEFAULT_YEARS, args));
} else if (day > 0) {
args.add(new Long(day));
args.add(new Long(hour));
args.add(sb);
args.add(null);
sb.append(tp.getText(DATETAG_PROPERTY_DAYS, DATETAG_DEFAULT_DAYS, args));
} else if (hour > 0) {
args.add(new Long(hour));
args.add(new Long(min));
args.add(sb);
args.add(null);
sb.append(tp.getText(DATETAG_PROPERTY_HOURS, DATETAG_DEFAULT_HOURS, args));
} else if (min > 0) {
args.add(new Long(min));
args.add(new Long(sec));
args.add(sb);
args.add(null);
sb.append(tp.getText(DATETAG_PROPERTY_MINUTES, DATETAG_DEFAULT_MINUTES, args));
} else {
args.add(new Long(sec));
args.add(sb);
args.add(null);
sb.append(tp.getText(DATETAG_PROPERTY_SECONDS, DATETAG_DEFAULT_SECONDS, args));
}
args.clear();
args.add(sb.toString());
if (date.before(now)) {
// looks like this date is passed
return tp.getText(DATETAG_PROPERTY_PAST, DATETAG_DEFAULT_PAST, args);
} else {
return tp.getText(DATETAG_PROPERTY_FUTURE, DATETAG_DEFAULT_FUTURE, args);
}
| public java.lang.String | getFormat()
return format;
| public java.lang.String | getName()
return name;
| public boolean | isNice()
return nice;
| public void | setFormat(java.lang.String format)
this.format = format;
| public void | setName(java.lang.String name)
this.name = name;
| public void | setNice(boolean nice)
this.nice = nice;
|
|