Struts 2 date data tag

<s:date>:

The <s:date> tag is used to format the date object by using custom formats or easily readable format using nice attribute. If nice attribute is set to true then it display the date in the easily readable format like an instant ago.

Syntax: <s:date name=”dateField” format=”dateFormat” />

Example:

index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>Struts 2 s:date data tag example</title>
	<META HTTP-EQUIV="Refresh" CONTENT="0;URL=Test.action">
  </head>  
  <body>
  </body>
</html>

test.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
	<head>
		<title>Struts 2 s:date data tag example</title>
	</head>
	<body>
		<h3>This is a s:date data tag example.</h3>
 
		<h4>Date in default format:</h4>
		<s:date name="currentDate" /><br/>
 
		<h4>Date in dd/MM/yyyy format:</h4>
		<s:date name="currentDate" format="dd/MM/yyyy"/><br/>
 
		<h4>Date in nice format:</h4>
		<s:date name="currentDate" nice="true"/><br/>
 
	</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 
 	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
        	org.apache.struts2.dispatcher.ng.
        	filter.StrutsPrepareAndExecuteFilter
          </filter-class>
	</filter>
 
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<welcome-file-list>
	  <welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<package name="default" extends="struts-default">
		<action name="Test" class="com.javawithease.action.Test">
			<result name="success">/test.jsp</result>
		</action>
	</package>
 
</struts>

Test.java

import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class.
 * @author javawithease
 */
public class Test extends ActionSupport {
	private Date currentDate;
 
	public String execute(){
		currentDate = new Date();
		return SUCCESS;
	}
 
	//getter setters
	public Date getCurrentDate() {
		return currentDate;
	}
 
	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}			
}

Output:

struts 51
 
Download this example.
 
Next Topic: Struts 2 param data tag with example.
Previous Topic: Struts 2 bean data tag with example.

Related Topics:

Struts 2 action tag with example.
Struts 2 include data tag with example.
Struts 2 bean data tag with example.
Struts 2 date data tag with example.
Struts 2 param data tag with example.
Struts 2 property data tag with example.
Struts 2 push data tag with example.
Struts 2 set data tag with example.
Struts 2 i18n and text data tags with example.
Struts 2 s:url and s:a data tags with example.