struts2 日付タグを使用して、以下の形式にする方法は次のとおりです。
- 2010 年 12 月 1 日
- 2010 年 12 月 2 日
- 2010 年 12 月 3 日
- 2010 年 12 月 5 日
アクションクラス:
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>
ただし、プレフィックス付きの日付が必要な場合は、手動でコードを処理する必要があります。
接尾辞を手動で追加するには、このリンクを確認してください。
s:date
struts2 usesの実装を確認したところ、SimpleDateFormatjava.util.SimpleDateFormat
で情報を見つけることができます。使用できるすべての形式が表示されますが、要件を満たすものはありません。したがって、解決策は、フォーマットを使用して自分で日付を文字列に解析する必要があります。
これがStruts2 Dateの実装です
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);
}
}
}
}