Struts 2 date tag example
Struts 2 “date” tag is used to format Date object in two ways :
- Custom date format (eg. “dd/MM/yyyy”).
- “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.
DateTagAction.java
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(); setCustomDate(newDate); 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 :
- Default date format.
- Custom date format.
- Easy readable notation.
date.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 date tag example</h1> <ol> <li> Default date format --> <strong><s:date name="customDate" /></strong> </li> <li> Date format in "dd/MM/yyyy" --> <strong><s:date name="customDate" format="dd/MM/yyyy" /></strong> </li> <li> In Date tag, set the nice attribute to "true" --> <strong><s:date name="customDate" nice="true" /></strong> </li> </ol> </body> </html>
3. struts.xml
Link it ~
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <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> </action> </package> </struts>
4. Demo
http://localhost:8080/Struts2Example/dateTagAction.action
Output







[...] date tag example Struts 2 date tag is used to format Date object in JSP page. [...]
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 struts.date.format 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.
Thanks,
Rushi