Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions?
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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.
 
Search Tutorials:
 
Software Solutions and Services
 

 
Google Custom Search:
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation

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.

 

                         


 

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,
Please help.. i m new to struts nas hibernate..
plz guide me how to get the records from the database and display it into the jsp table..

Posted by shobhit dimri on Monday, 09.22.08 @ 13:28pm | #80592

Hi Mike,
Please try that::

<field property="dateQuery" depends="required,date">
<arg0 key="errors.fieldFormat"/>
<var><var-name>datePatternStrict</var-name><var-value>dd/MM/yyyy</var-value></var>
</field>


<!-- Also you can try that way too -->
<field property="dateQuery" depends="mask">
<msg name="mask" key="errors.fieldFormat"/>
<var>
<var-name>mask</var-name>
<var-value>^0[0-9]{9}</var-value>
</var>
</field>

Posted by Sohel Taslim on Monday, 03.31.08 @ 15:10pm | #54859

hello i wanna validate a date as dd/MM/yyyy
i tried to put in my validation.xml

<field name="dateQuery">
<field-validator type="regex">
<param name="expression">
[0-9][0-9]/[0-9][0-9]/[1-9][0-9][0-9][0-9]
</param>
<message key="errors.fieldFormat" />
</field-validator>

but no message appear!!!!
how can i do
</field>

Posted by Mike on Tuesday, 03.11.08 @ 17:31pm | #52301

Thanks a lot.
You saved my time.

Posted by Sohel Taslim on Monday, 12.3.07 @ 10:13am | #41157

Training Courses
Tell A Friend
Your Friend Name
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.