Struts 2 date tag example

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
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: Blue Coaster33()

  • velu

    Hi mkyong,

    Thanks for your post, Thank you very much.

  • Mohaideen

    Hai MKyoung ,
    I Struts 2 , is there any possiblities to do pagination ,
    Is there any predefined tags avalible for it ?
    Many website shows as display tag can be used to pagination ,
    but I dont think so , I tried my best with it ,

    it is working only with struts 1.x.

    Can u please include an article about pagination in struts 2 ?


  • Rajiv


    In the struts page there is a Note available: if date object is not present in the value stack then returns empty. For this case I have some params.accdate coming from session and it doesn’t available in the value stack then how can we make use of tag for this value.

    Rajiv Jain

  • Muthu

    Hi yong,
    In this tutorial where we are calling the action dateTagAction…. Pls clarify me..

  • abdul

    how can i apply German format tt.MM.jjjj in struts date tag

  • Pingback: Struts 2 Tutorials()

  • Rushi Jukar

    Hy Mkyong,
    I have really learnt a lot about struts 2 from your tutorials. But I’m having some trouble with taking date input in my POC application. Hope you can find some time to help me out.

    My problem:
    I’m using the modeldriven method, one of the feild is a date. when I run the application from IE it takes almost any date format (eg dd/MM/yyyy dd-MMM-yyyy dd.MM.yy)
    But when I run the same application from firefox (portable version) only MM/dd/yyyy works (any other format give error message “Invalid value enter for xxx”). I’m guessing this is bcoz its a portable version and locale must be set to en-us.

    Now, is there some setting by which I can set the locale or date format for my application. I’ve tried in the properties, but it only affects the tag and not the input format.

    What would be the best way to fix my date input format irrespective of locale? eg en-uk

    if you can give a sample code then that would be great or can you atleast point me in the right direction.