In this example, We will discuss about the different type of date format using struts2.2.1.
![]() |
1- index.jsp
<html>ADS_TO_REPLACE_1 <head> <title>Date Format Example</title> </head>ADS_TO_REPLACE_2 <body> <h2>Date Format Example</h2> <hr>Current dateADS_TO_REPLACE_3 <a href="CurrenDate.action">Current date</a> <hr> Date in different format <a href="CurrenDateInDiffFormat.action">CurrenDateInDiffFormat</a>ADS_TO_REPLACE_4 </body> </html> |
2-DateAction.java
package roseindia.action;ADS_TO_REPLACE_5 import java.util.Calendar; import java.util.Date; import com.opensymphony.xwork2.ActionSupport;ADS_TO_REPLACE_6 public class DateAction extends ActionSupport { private Date currentDate = new Date(); private Date pastDate;ADS_TO_REPLACE_7 private Calendar caldate = Calendar.getInstance(); public String execute() { caldate.set(1986, 9, 25);ADS_TO_REPLACE_8 pastDate = caldate.getTime(); return SUCCESS; }ADS_TO_REPLACE_9 public Date getCurrentDate() { return currentDate; }ADS_TO_REPLACE_10 public Date getPastDate() { return pastDate; }ADS_TO_REPLACE_11 public void setPastDate(Date pastDate) { this.pastDate = pastDate; }ADS_TO_REPLACE_12 } |
3-CurrentDate.jsp
<%@taglib uri="/struts-tags" prefix="s"%> <html>ADS_TO_REPLACE_13 <head> <title>Date Format Example</title> </head>ADS_TO_REPLACE_14 <body> <h2>Date Format Example</h2> <hr>ADS_TO_REPLACE_15 Current Date : <s:date name="currentDate" format="yyyy-MM-dd"></s:date> <hr>ADS_TO_REPLACE_16 Date in different format <a href="CurrenDateInDiffFormat.action">Current Date In Different Format</a>ADS_TO_REPLACE_17 </body> </html> |
4 struts.xml
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" />ADS_TO_REPLACE_18 <constant name="struts.devMode" value="false" /> <package name="roseindia" extends="struts-default" namespace="/" > <action name="CurrenDate" class="roseindia.action.DateAction">ADS_TO_REPLACE_19 <result name="success">/jsp/CurrentDate.jsp</result> </action> <action name="CurrenDateInDiffFormat" class="roseindia.action.DateAction">ADS_TO_REPLACE_20 <result name="success">/jsp/DateInDifferentFormat.jsp</result> </action> </package>ADS_TO_REPLACE_21 </struts> |
5 DateInDifferentFormat.jsp
<%@ taglib uri="/struts-tags" prefix="s" %> <html> <head>ADS_TO_REPLACE_22 <title>DateFormatExample </title> </head> <body>ADS_TO_REPLACE_23 Current Date : <s:date name="currentDate" format="yyyy-MM-dd"></s:date> <h2>Date Format Example</h2>ADS_TO_REPLACE_24 <hr> <b>Date Format(EEEE yyyy-MM-dd):</b><br/><br/> <s:date name="pastDate" format="EEEE yyyy-MM-dd" />ADS_TO_REPLACE_25 <hr> <b>Date Format (EEEE MM/dd/yyyy hh:mm AM/PM):</b><br/><br/> <s:date name="pastDate" format="EEEE MM/dd/yyyy 'at' hh:mm a" />ADS_TO_REPLACE_26 <hr> <b>Date Format (EEEE dd-MMM-yyyy hh:mm AM/PM) :</b><br/><br/> <s:date name="pastDate" format="EEEE dd-MMM-yyyy 'at' hh:mm a" />ADS_TO_REPLACE_27 <hr> <b> Format (EEEE dd/MM/yyyy hh:mm) :</b><br/><br/> <s:date name="pastDate" format="EEEE dd/MM/yyyy hh:mm" />ADS_TO_REPLACE_28 <hr> <b>Date Format(Nice):</b><br/><br/> <s:date name="pastDate" format="yyyy-MM-dd" nice="true"/>ADS_TO_REPLACE_29 <hr> </body> </html> |
index.jspADS_TO_REPLACE_30
currentDate.jsp
ADS_TO_REPLACE_31
StrutsExample.gif
Download Select Source CodeADS_TO_REPLACE_32
Advertisements
Ads
Ads