<f:convertDateTime>標(biāo)簽用于將用戶輸入轉(zhuǎn)換為指定的日期。 您可以通過(guò)將組件標(biāo)簽內(nèi)的convertDateTime標(biāo)簽嵌套來(lái)將組件的數(shù)據(jù)轉(zhuǎn)換為java.util.Date。 convertDateTime標(biāo)簽有幾個(gè)屬性,可以指定數(shù)據(jù)的格式和類(lèi)型。
| 屬性 | 類(lèi)型 | 描述 |
|---|---|---|
| binding | DateTimeConverter | 它用于將轉(zhuǎn)換器綁定到受委托Bean屬性。 |
| dateStyle | String | 它用于定義由java.text.DateFormat指定的日期或日期字符串的日期部分的格式。 只適用于type是date或both,如果pattern未定義。 有效值:default,short,medium,long和full。 如果沒(méi)有指定值,則使用默認(rèn)值。 |
| for | String | 它用于引用該標(biāo)簽嵌套在其中的復(fù)合組件內(nèi)的一個(gè)對(duì)象。 |
| locale | String 或 Locale | 它是一個(gè)區(qū)域設(shè)置的實(shí)例,它在格式化或解析期間使用了日期和時(shí)間的預(yù)定義樣式。 如果未指定,將使用FacesContext.getLocale返回的區(qū)域設(shè)置。 |
| pattern | String | 它用于自定義格式化模式,用于確定如何格式化和解析日期/時(shí)間字符串。 如果指定了此屬性,則將忽略dateStyle,timeStyle和type屬性。 |
| timeStyle | String | 它用于定義由java.text.DateFormat指定的時(shí)間或日期字符串的時(shí)間部分的格式。 僅當(dāng)類(lèi)型為時(shí)間和模式未定義時(shí)才應(yīng)用。有效值:default,short,medium,long和full。 如果沒(méi)有指定值,則使用默認(rèn)值。 |
| timeStyle | String | 它用于定義由java.text.DateFormat指定的時(shí)間或日期字符串的時(shí)間部分的格式。 僅當(dāng)類(lèi)型為時(shí)間和模式未定義時(shí)才應(yīng)用。有效值:default,short,medium,long和full。如果沒(méi)有指定值,則使用默認(rèn)值。 |
| timeZone | String 或 TimeZone | 它用于解釋日期字符串中任何時(shí)間信息的時(shí)區(qū)。 |
| type | String | 它用于指定字符串值是否包含日期,時(shí)間或兩者。有效值是日期,時(shí)間或兩者。 如果未指定值,則使用日期。 |
<f:convertDateTime>實(shí)例打開(kāi)NetBeans IDE創(chuàng)建一個(gè)Web工程:convertDateTime,其目錄結(jié)構(gòu)如下所示 -

創(chuàng)建以下文件代碼,文件:index.xhtml 的代碼內(nèi)容如下所示 -
<?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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:outputLabel for="username">User Name</h:outputLabel>
<h:inputText id="user-id" value="#{user.name}"/><br/>
<h:outputLabel for="age">Date of Birth</h:outputLabel>
<h:inputText id="dob-id" value="#{user.dob}" converterMessage="Please provide date of birth in yyyy-mm-dd format">
<f:convertDateTime pattern="yyyy-mm-dd" />
</h:inputText><br/>
<h:commandButton action="result.xhtml" value="Submit"/>
</h:form>
</h:body>
</html>
文件:result.xhtml 的代碼內(nèi)容如下所示 -
<?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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1> Hello,
<h:outputText value="#{user.name}"/>
</h1>
<h:outputLabel>Your date of birth is: </h:outputLabel>
<h:outputText value="#{user.dob}">
<f:convertDateTime pattern="yyyy-mm-dd"/>
</h:outputText>
</h:body>
</html>
文件:User.java 的代碼內(nèi)容如下所示 -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yiibai;
/**
*
* @author Administrator
*/
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class User {
String name;
Date dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
右鍵運(yùn)行工程:convertDateTime,如果沒(méi)有任何錯(cuò)誤,打開(kāi)瀏覽器訪問(wèn):
http://localhost:8084/convertDateTime/
應(yīng)該會(huì)看到以下結(jié)果 -
簡(jiǎn)單寫(xiě)入一些信息,然后提交 -

<f:convertDateTime>實(shí)例2打開(kāi)NetBeans IDE創(chuàng)建一個(gè)Web工程:convertDateTime2,其目錄結(jié)構(gòu)如下所示 -

創(chuàng)建以下文件代碼,文件:index.xhtml 的代碼內(nèi)容如下所示 -
<?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"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:outputLabel for="username">User Name</h:outputLabel>
<h:inputText id="user-id" value="#{user.name}"/><br/>
<h:outputLabel for="age">Date of Birth</h:outputLabel>
<h:inputText id="dob-id" value="#{user.dob}">
<f:convertDateTime pattern="yyyy-mm-dd"/>
</h:inputText>
<br/>
<h:commandButton action="result.xhtml" value="Submit"/>
</h:form>
</h:body>
</html>
文件:result.xhtml 的代碼內(nèi)容如下所示 -
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Response Page</title>
</h:head>
<h:body>
<h1> Hello,
<h:outputText value="#{user.name}"/>
</h1>
<h:outputLabel value="Your date of birth in different-different formats is given below:"></h:outputLabel><br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime type="date" dateStyle="medium"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime type="date" dateStyle="full"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime type="time" dateStyle="full"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime type="date" pattern="dd/mm/yyyy"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime dateStyle="full" pattern="yyyy-mm-dd"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime dateStyle="full" pattern="yyyy.MM.dd 'at' HH:mm:ss z"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime dateStyle="full" pattern="h:mm a"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime dateStyle="long" timeZone="EST" type="both"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime locale="de" timeStyle="long" type="both" dateStyle="full"/>
</h:outputText>
<br/>
<h:outputText value="#{user.dob}">
<f:convertDateTime locale="en" timeStyle="short" type="both" dateStyle="full"/>
</h:outputText>
</h:body>
</html>
文件:User.java 的代碼內(nèi)容如下所示 -
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.yiibai;
/**
*
* @author Administrator
*/
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class User {
String name;
Date dob;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
}
右鍵運(yùn)行工程:convertDateTime2,如果沒(méi)有任何錯(cuò)誤,打開(kāi)瀏覽器訪問(wèn):
http://localhost:8084/convertDateTime2/
應(yīng)該會(huì)看到以下結(jié)果 -
簡(jiǎn)單寫(xiě)入一些信息,然后提交 -
