Struts 2 Date Format Examples

In this tutorial you will learn about Date Format function in Struts 2. We have provided fully tested example code to illustrate the concepts. You can copy the code and use it for your fast development.

Struts 2 Date Format Examples

Struts 2 Date Format Examples

     

In this tutorial you will learn about Date Format function in Struts 2. We have provided fully tested example code to illustrate the concepts. You can copy the code and use it for your fast development.

Struts Date tag

Struts date tag is used to output the date into required format. It can be used to format the date into several different formats. Syntax of the date tag is:

<s:date name="todayDate" format="yyyy-MM-dd" />

Parameters

Following table shows the parameters of date tag.

Name Required Default Evaluated Type Description
format false   false String The date format pattern
id false   true String It will be used id tag in the HTML 
name true   true String The date value which is to be formatted
nice false false true Boolean Whether to print out the date nicely

The nice format is very interesting. The following table shows, how it will format the date output:

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}

Writing Example

In this example we will use DateBean action class to get the current date of the system. This current date will be used in our example. Here is the code of DateBean.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;


/**
 <p> Validate a user login. </p>
 */
public  class DateBean  extends ActionSupport {

  

  public String execute() throws Exception {

  setTodayDate(new Date());
  return SUCCESS;

  }


  // ---- Username property ----

  /**
 <p>Field to store Today's Date.</p>
 <p/>
 */
  private Date todayDate;


  /**
 <p>Provide Today's Date.</p>
 *
 @return Returns the Todays date.
 */
  public Date getTodayDate() {
  return todayDate;
  }

  /**
 <p>Store new Date</p>
 *
 @param value The username to set.
 */
  public void setTodayDate(Date value) {
  todayDate = value;
  }

}

Add the following entry in the struts.xml file in order to create action mapping:

<action name="FormatDate" class="net.roseindia.DateBean">
<result>/pages/dateformat.jsp</result>
</action>

Following jsp file(dateformat.jsp) shows the usage of date tag:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Format Date Example!</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>


<s:form action="FormatDate" method="POST">


<tr>
<td>Date in (yyyy-MM-dd) Format:</td>
<td>
<b><s:date name="todayDate" format="yyyy-MM-dd" /></b>
</td>
</tr>

<tr>
<td>Date in (MM/dd/yyyy hh:mm AM/PM) Format:</td>
<td>
<b><s:date name="todayDate" format="MM/dd/yyyy 'at' hh:mm a" /></b>
</td>
</tr>

<tr>
<td>Date in (dd-MMM-yyyy hh:mm AM/PM) Format:</td>
<td>
<b><s:date name="todayDate" format="dd-MMM-yyyy 'at' hh:mm a" /></b>
</td>
</tr>

<tr>
<td>Date in (dd/MM/yyyy hh:mm) Format:</td>
<td>
<b><s:date name="todayDate" format="dd/MM/yyyy hh:mm" /></b>
</td>
</tr>

<tr>
<td>Nice Format:</td>
<td>
<b><s:date name="todayDate" format="yyyy-MM-dd" nice="true"/></b>
</td>
</tr>

</s:form>

</body>

</html>

Output of the program:

 

In this section you learnt how to use Struts 2 Date format tag.