Struts 2 “date” tag is used to format Date object in two ways :

  1. Custom date format (eg. “dd/MM/yyyy”).
  2. “nice” attribute to format date into an easy readable notations like “this date is 162 days ago”.

In this tutorial, it shows the use of Struts 2 “date” tag to format a Date object into “custom date format” and “easy readable notations“.

1. Action

An Action class to forward the request, and initialize a Date object with a predefined date.

package com.mkyong.common.action;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
public class DateTagAction extends ActionSupport{
	public Date customDate;
	public String execute() {
		Calendar cal = Calendar.getInstance();
		//set date to january 31, 2010
		cal.set(2010, 0, 31);
		Date newDate = cal.getTime();
		return SUCCESS;

	public Date getCustomDate() {
		return customDate;

	public void setCustomDate(Date customDate) {
		this.customDate = customDate;

2. date tag example

A JSP page to show the use of “date” tag to format a Date object in :

  1. Default date format.
  2. Custom date format.
  3. Easy readable notation.


<%@ taglib prefix="s" uri="/struts-tags" %>
<h1>Struts 2 date tag example</h1>


Default date format
--> <strong><s:date name="customDate" /></strong>

Date format in "dd/MM/yyyy"
--> <strong><s:date name="customDate" format="dd/MM/yyyy" /></strong>

In Date tag, set the nice attribute to "true"
--> <strong><s:date name="customDate" nice="true" /></strong>



3. struts.xml

Link it ~

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 	<constant name="struts.devMode" value="true" />
	<package name="default" namespace="/" extends="struts-default">
		<action name="dateTagAction" 
			class="com.mkyong.common.action.DateTagAction" >
			<result name="success">pages/date.jsp</result>

4. Demo



Struts 2 date tag example


  1. Struts 2 date tag documentation