0

How can we use the struts2 date tag to have the format below:

  • 1st December 2010
  • 2nd December 2010
  • 3rd December 2010
  • 5th December 2010
JR Galia
  • 17,229
  • 19
  • 92
  • 144

2 Answers2

1

Action class :

       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>

But if you want the date with prefixes, you need to do some manual code work.

check this link for adding the suffixes manually.

How do you format the day of the month to say "11th", "21st" or "23rd" in Java? (ordinal indicator)

Community
  • 1
  • 1
Srinivas B
  • 1,821
  • 4
  • 17
  • 35
  • it display `22 years, 115 days ago` – JR Galia Nov 21 '12 at 07:51
  • 1
    @JRGalia: I have changed my answer to your date format. But if you want to add the suffixes like, 'st','nd','rd', you have to do it manually. follow this link for adding prefixes. http://stackoverflow.com/questions/4011075/how-do-you-format-the-day-of-the-month-to-say-11th-21st-or-23rd-in-java – Srinivas B Nov 21 '12 at 09:22
  • @AndreaLigos: Thanks for your comment. Added the link to the answer – Srinivas B Nov 21 '12 at 09:42
1

I just checked the implementation of s:date, struts2 uses java.util.SimpleDateFormat, you can find the info here SimpleDateFormat, it shows all the format you can use, none of them fills your requirement. so the solution should be parse the Date into String by yourself with your format.

Here is Struts2 Date Implementation

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);
                    }
                }
            }
        }
Jaiwo99
  • 9,687
  • 3
  • 36
  • 53