JSF 2 f:convertDateTime Tag

JSF 2 f:convertDateTime Tag


Posted in : Java Posted on : May 31, 2012 at 11:00 AM Comments : [ 0 ]

In this tutorial you will learn about how to convert String into a specified date/time.

JSF 2 f:convertDateTime Tag

In this tutorial you will learn about how to convert String into a specified date/time.

convertDateTime tag of JSF core tag library is used to convert the date string into a specified date/time format. This tag should be nested into the tag onto which you want to implement the date validation. In this tag various of optional attributes can also be used for formatting the date/time through the use of various styles and time zones. This tag is a standard tag of JSF that converts the date string to a specific date/time.

Attributes of convertDateTime

  • dateStyle : This is not a required attribute. This attribute specifies the predefined date style format into which you want to format the date string. It parses the date string into the specified dateStyle of the date component. But this attribute is applied only if type attribute is specified to "date" or "both" and its default value is "default".
  • locale : This is not a required attribute. This attribute specifies the predefined styles for dates and times to format or parse the date component.
  • pattern : This is not a required attribute but is used to format the date/time of date component in a customized formatting pattern.
  • timeStyle : This is not a required attribute of this tag but, is used to format and parse the time component of date string into a predefined formatting style and it is applied only when the type attribute is specified to "time" or "both" and its default value is "default".
  • timeZone : This is not a required attribute of this tag but, it is used to specify the Time Zone. In this Time Zone the time information will be interpreted to a date string.
  • type : This is not a required attribute but, is used for specifying the type of string value into which the date string will be format or parsed. It could be a "date", "time", and "both". Default value of the type attribute is "date".
  • binding : This is not a required attribute. This attribute specifies a ValueExpression which evaluates to an instance of javax.faces.convert.DateTimeConverter.
  • for : This is not a required attribute. This attribute refers to the value as object specified in the components. It may use any of the specified object value of the tag inside which this tag is nested.

Example :

Here I am giving a simple example which will demonstrate you about the <f:convertDateTime> tag of JSF core tag library. In this example I have created a dynamic web project named jsfConvertDateTime. Then created a package into the src folder in Eclipse and then created a Java bean class inside this package named DateBean into which there is a data member date and its setter getter methods to set and get its value. Then created a folder jspPages to keep the JSP files into the WebContent folder in Eclipse. Then created JSP pages to take input the date string into the textbox in one JSP page and displayed that string into a specified format on the other JSP page. Then created a web.xml file to map the Servlet. and finally created a index.jsp page which will be called first when you will deploy your web project.

Directory Structure of this web project :

DateBean.java

package devmanuals;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="dateBean")
@RequestScoped

public class DateBean {
Date date;

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}

inputDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Input Date</title>
</head>
<body>
<f:view>
<h:form>
Enter Date : <h:inputText id="dt" value="#{dateBean.date}">
<f:convertDateTime pattern="dd-MM-yyyy"/>
</h:inputText>
<br>
<h:message for="dt" style="color:red" /><br><br>
<h:commandButton value="Find Date" action="outputDate"></h:commandButton>
</h:form>
</f:view>
</body>
</html>

outputDate.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Output Date</title>
</head>
<body>
<f:view>
Specified Date pattern is :
<h:outputText value="#{dateBean.date}">
<f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>
</f:view> 
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<jsp:forward page="/jspPages/inputDate.jsf"></jsp:forward>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsfConvertDateTime</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
</web-app>

Output :

1. When you will execute the above example ( http://localhost:8080/jsfConvertDateTime/) first page will be open as follows :

2. When you will input the string as specified to the input text box validation (done on inputDate.jsp page at <h:inputText id="dt" value="#{dateBean.date}">
<f:convertDateTime pattern="dd-MM-yyyy"/>) then the output will be as follows :

3. But if you will input the value other than this format and other string then the output will be as follows :

4. And if you will not validate the date format of inputText at the time of coding then the output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics