0

我们如何使用 struts2 日期标签具有以下格式:

  • 2010 年 12 月 1 日
  • 2010 年 12 月 2 日
  • 2010 年 12 月 3 日
  • 2010 年 12 月 5 日
4

2 回答 2

1

我刚刚检查了s:datestruts2 uses的实现java.util.SimpleDateFormat,你可以在这里找到信息SimpleDateFormat,它显示了你可以使用的所有格式,它们都不符合你的要求。所以解决方案应该使用您的格式将 Date 解析为 String

这是 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);
                    }
                }
            }
        }
于 2012-11-21T08:29:33.557 回答
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>

但是如果你想要带前缀的日期,你需要做一些手动代码工作。

检查此链接以手动添加后缀。

如何在 Java 中将月份中的日期格式化为“11 日”、“21 日”或“23 日”?(序数指标)

于 2012-11-21T07:31:41.917 回答