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