Struts 2- Date Example
Aim:
To format the date. To learn how to format the date. There are many format available in struts2.
There are many forms:
Format:
A). yyyy-MM-dd
B). MM/dd/yyyy 'at' hh:mm a
C). dd-MMM-yyyy 'at' hh:mm a
D). dd/MM/yyyy hh:mm
E). yyyy-MM-dd
Procedure:
Step 1:
Create action support.
Name: DateBean
Right click on example under source packages.
New – other – struts2 – struts2 action.
Next—give class name like DateBean.
You know how to create struts2 action using netbean6.8. if you forget the steps to create the struts2 action. Refer the previous post and continue this post.
Include the following code into the DateBean
package example;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class DateBean extends ActionSupport
{
private Date todate;
public Date getTodate()
{
return todate;
}
public void setTodate(Date todate)
{
this.todate = todate;
}
public DateBean()
{
}
public String execute() throws Exception
{
setTodate(new Date());
return "success";
}
}
Step 2:
Create jsp file.
Name: index.jsp
Right click on example under web pages.
New – other—select web under catogories – select JSP under file types
Next
Give jsp file name like index
Include following the code into the index.jsp
< %@taglib uri="/struts-tags" prefix="s" % >
< html >
< head >
< title > Date Example < /title >
< s:head theme="ajax"/ >
< /head >
< body >
< s:datetimepicker name="todate" label="Format (yyyy-MM-dd)" displayFormat="yyyy-MM-dd"/ >
< s:datetimepicker name="todate" label="Format (dd-MMM-yyyy)" displayFormat="dd-MMM-yyyy"/ >
< /body >
< /html >
Step 3:
Change in struts.xml like below
< !DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
http://struts.apache.org/dtds/struts-2.0.dtd >
< struts >
< package name="example" namespace="/example" extends="struts-default" >
< action name="FormDate" class="example.DateBean" >
< result name="success" < /example/index.jsp < /result >
< /action >
< /package >
< /struts >
Step 4:
Run the index.jsp
Conclusion:
To understand how to display the date from this post. You will to try the examine the different format