JSF轉換日期時間

<f:convertDateTime>標籤用於將用戶輸入轉換為指定的日期。 您可以通過將組件標籤內的convertDateTime標籤嵌套來將組件的數據轉換為java.util.DateconvertDateTime標籤有幾個屬性,可以指定數據的格式和類型。

標籤屬性

屬性 類型 描述
binding DateTimeConverter 它用於將轉換器綁定到受委託Bean屬性。
dateStyle String 它用於定義由java.text.DateFormat指定的日期或日期字串的日期部分的格式。 只適用於typedateboth,如果pattern未定義。 有效值:defaultshortmediumlongfull。 如果沒有指定值,則使用默認值。
for String 它用於引用該標籤嵌套在其中的複合組件內的一個對象。
locale String 或 Locale 它是一個區域設置的實例,它在格式化或解析期間使用了日期和時間的預定義樣式。 如果未指定,將使用FacesContext.getLocale返回的區域設置。
pattern String 它用於自定義格式化模式,用於確定如何格式化和解析日期/時間字串。 如果指定了此屬性,則將忽略dateStyletimeStyletype屬性。
timeStyle String 它用於定義由java.text.DateFormat指定的時間或日期字串的時間部分的格式。 僅當類型為時間和模式未定義時才應用。有效值:defaultshortmediumlongfull。 如果沒有指定值,則使用默認值。
timeStyle String 它用於定義由java.text.DateFormat指定的時間或日期字串的時間部分的格式。 僅當類型為時間和模式未定義時才應用。有效值:defaultshortmediumlongfull。如果沒有指定值,則使用默認值。
timeZone String 或 TimeZone 它用於解釋日期字串中任何時間資訊的時區。
type String 它用於指定字串值是否包含日期,時間或兩者。有效值是日期,時間或兩者。 如果未指定值,則使用日期。

JSF <f:convertDateTime>實例

打開NetBeans IDE創建一個Web工程:convertDateTime,其目錄結構如下所示 -

創建以下檔代碼,檔:index.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"
      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 的代碼內容如下所示 -

<?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 的代碼內容如下所示 -

/*
 * 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.zaixian;

/**
 *
 * @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;
    }
}

右鍵運行工程:convertDateTime,如果沒有任何錯誤,打開流覽器訪問:

http://localhost:8084/convertDateTime/

應該會看到以下結果 -

簡單寫入一些資訊,然後提交 -

JSF <f:convertDateTime>實例2

打開NetBeans IDE創建一個Web工程:convertDateTime2,其目錄結構如下所示 -

創建以下檔代碼,檔:index.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"
      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 的代碼內容如下所示 -

<?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 的代碼內容如下所示 -

/*
 * 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.zaixian;

/**
 *
 * @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;
    }
}

右鍵運行工程:convertDateTime2,如果沒有任何錯誤,打開流覽器訪問:

http://localhost:8084/convertDateTime2/

應該會看到以下結果 -

簡單寫入一些資訊,然後提交 -


上一篇: JSF <f:convertNumber>標籤 下一篇: JSF自定義轉換器