- JSF Tutorial
- JSF - Home
- JSF - Overview
- JSF - Environment Setup
- JSF - Architecture
- JSF - Life Cycle
- JSF - First Application
- JSF - Managed Beans
- JSF - Page Navigation
- JSF - Basic Tags
- JSF - Facelet Tags
- JSF - Convertor Tags
- JSF - Validator Tags
- JSF - DataTable
- JSF - Composite Components
- JSF - Ajax
- JSF - Event Handling
- JSF - JDBC Integration
- JSF - Spring Integration
- JSF - Expression Language
- JSF - Internationalization
- JSF Useful Resources
- JSF - Quick Guide
- JSF - Useful Resources
- JSF - Discussion
JSF - f:convertDateTime
f:convertDateTime tag is used to convert a string value to a date of required format. It also acts as a validator, a required date format.
JSF Tag
<f:convertDateTime pattern = "dd-mm-yyyy" />
Tag Attributes
S.No | Attribute & Description |
---|---|
1 | type date (default), time, or both |
2 | dateStyle default, short, medium, long, or full |
3 | timeStyle default, short, medium, long, or full |
4 | pattern Formatting pattern, as defined in java.text.SimpleDateFormat |
5 | locale Locale whose preferences are to be used for parsing and formatting |
6 | timeZone
Time zone to use for parsing and formatting |
Example Application
Let us create a test JSF application to test the above tag.
Step | Description |
---|---|
1 | Create a project with a name helloworld under a package com.tutorialspoint.test as explained in the JSF - First Application chapter. |
2 | Modify home.xhtml as explained below. Keep the rest of the files unchanged. |
3 | Create result.xhtml in the webapps directory as explained below. |
4 | Create UserData.java as a managed bean under package com.tutorialspoint.test as explained below. |
5 | Compile and run the application to make sure business logic is working as per the requirements. |
6 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
7 | Launch your web application using appropriate URL as explained below in the last step. |
UserData.java
package com.tutorialspoint.test; import java.io.Serializable; import java.util.Date; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name = "userData", eager = true) @SessionScoped public class UserData implements Serializable { private static final long serialVersionUID = 1L; public Date date; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } }
home.xhtml
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml" xmlns:h = "http://java.sun.com/jsf/html" xmlns:f = "http://java.sun.com/jsf/core"> <h:head> <title>JSF tutorial</title> </h:head> <h:body> <h2>ConvertDateTime Example</h2> <h:form> <h:inputText id = "dateInput" value = "#{userData.date}" label = "Date" > <f:convertDateTime pattern = "dd-mm-yyyy" /> </h:inputText> <h:commandButton value = "submit" action = "result"/> </h:form> <br/> <h:message for = "dateInput" style = "color:red" /> <h:outputText value = "12-01-2012" > <f:convertDateTime pattern = "dd-mm-yyyy" /> </h:outputText> </h:body> </html>
result.xhtml
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns = "http://www.w3.org/1999/xhtml" xmlns:f = "http://java.sun.com/jsf/core" xmlns:h = "http://java.sun.com/jsf/html" xmlns:ui = "http://java.sun.com/jsf/facelets"> <h:body> <h2>Result</h2> <hr /> #{userData.date} </h:body> </html>
Once you are ready with all the changes done, let us compile and run the application as we did in JSF - First Application chapter. If everything is fine with your application, this will produce the following result.
Enter any invalid value and press Submit button. See the following error message.
Enter any valid value and press Submit button. See the following result.