我们如何使用struts2日期标记来获得以下格式:
答案 0 :(得分:1)
动作类:
public String execute() {
Calendar cal = Calendar.getInstance();
//set date to january 31, 2010
cal.set(2010, 0, 31);
Date newDate = cal.getTime();
setCustomDate(newDate);
return SUCCESS;
}
public Date getCustomDate() {
return customDate;
}
public void setCustomDate(Date customDate) {
this.customDate = customDate;
}
JSP:
<li>
Date format in "dd MMMMM yyyy"
--> <strong><s:date name="todayDate" format="dd MMMMM yyyy" /></strong>
</li>
但是如果你想要带有前缀的日期,你需要做一些手动代码工作。
检查此链接是否手动添加后缀。
How do you format the day of the month to say "11th", "21st" or "23rd" in Java? (ordinal indicator)
答案 1 :(得分:1)
我刚检查了s:date
的实现,struts2使用了java.util.SimpleDateFormat
,你可以在这里查找信息SimpleDateFormat,它显示了你可以使用的所有格式,它们都没有满足你的要求。所以解决方案应该自己用你的格式解析日期到字符串。
这是Struts2日期实施
if (date != null) {
TextProvider tp = findProviderInStack();
if (tp != null) {
if (nice) {
msg = formatTime(tp, date);
} else {
TimeZone tz = getTimeZone();
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)) {
SimpleDateFormat sdf = new SimpleDateFormat(globalFormat,
ActionContext.getContext().getLocale());
sdf.setTimeZone(tz);
msg = sdf.format(date);
} else {
DateFormat df = DateFormat.getDateTimeInstance(
DateFormat.MEDIUM, DateFormat.MEDIUM,
ActionContext.getContext().getLocale());
df.setTimeZone(tz);
msg = df.format(date);
}
} else {
SimpleDateFormat sdf = new SimpleDateFormat(format, ActionContext
.getContext().getLocale());
sdf.setTimeZone(tz);
msg = sdf.format(date);
}
}
if (msg != null) {
try {
if (getVar() == null) {
writer.write(msg);
} else {
putInContext(msg);
}
} catch (IOException e) {
LOG.error("Could not write out Date tag", e);
}
}
}
}