鍍金池/ 教程/ HTML/ JSF轉(zhuǎn)換日期時(shí)間
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標(biāo)簽
JSF應(yīng)用程序入門示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復(fù)合組件
JSF <h:inputText>標(biāo)簽
JSF表單組合框
JSF <h:messages>標(biāo)簽
JSF <h:message>標(biāo)簽
JSF轉(zhuǎn)換日期時(shí)間
JSF JDBC連接
JSF <h:inputHidden>標(biāo)簽
JSF多選列表框
JSF <h:inputSecret>標(biāo)簽
JSF自定義轉(zhuǎn)換器
JSF <f:ajax>標(biāo)簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標(biāo)簽
JSF驗(yàn)證器標(biāo)簽
JSF驗(yàn)證字符串長度
JSF轉(zhuǎn)換器標(biāo)簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實(shí)例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標(biāo)簽
JSF <f:convertNumber>標(biāo)簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標(biāo)簽
JSF操作事件
JSF驗(yàn)證正則表達(dá)式
JSF數(shù)據(jù)表(h:dataTable)行號
JSF <h:setPropertyActionListener>標(biāo)簽
JSF注入托管bean實(shí)例
JSF <h:commandButton>標(biāo)簽
JSF Web資源
JSF <h:inputFile>標(biāo)簽
JSF驗(yàn)證浮點(diǎn)數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點(diǎn))
JSF自定義驗(yàn)證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標(biāo)記
JSF表單復(fù)選框(CheckBox)示例
JSF <h:form>標(biāo)簽
JSF Facelets技術(shù)介紹
JSF輸出格式化
JSF <h:inputtextarea>標(biāo)簽
JSF驗(yàn)證整數(shù)范圍
JSF <h:panelGrid>標(biāo)簽

JSF轉(zhuǎn)換日期時(shí)間

<f:convertDateTime>標(biāo)簽用于將用戶輸入轉(zhuǎn)換為指定的日期。 您可以通過將組件標(biāo)簽內(nèi)的convertDateTime標(biāo)簽嵌套來將組件的數(shù)據(jù)轉(zhuǎn)換為java.util.DateconvertDateTime標(biāo)簽有幾個屬性,可以指定數(shù)據(jù)的格式和類型。

標(biāo)簽屬性

屬性 類型 描述
binding DateTimeConverter 它用于將轉(zhuǎn)換器綁定到受委托Bean屬性。
dateStyle String 它用于定義由java.text.DateFormat指定的日期或日期字符串的日期部分的格式。 只適用于typedateboth,如果pattern未定義。 有效值:default,short,medium,longfull。 如果沒有指定值,則使用默認(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,timeStyletype屬性。
timeStyle String 它用于定義由java.text.DateFormat指定的時(shí)間或日期字符串的時(shí)間部分的格式。 僅當(dāng)類型為時(shí)間和模式未定義時(shí)才應(yīng)用。有效值:default,short,medium,longfull。 如果沒有指定值,則使用默認(rèn)值。
timeStyle String 它用于定義由java.text.DateFormat指定的時(shí)間或日期字符串的時(shí)間部分的格式。 僅當(dāng)類型為時(shí)間和模式未定義時(shí)才應(yīng)用。有效值:default,shortmedium,longfull。如果沒有指定值,則使用默認(rèn)值。
timeZone String 或 TimeZone 它用于解釋日期字符串中任何時(shí)間信息的時(shí)區(qū)。
type String 它用于指定字符串值是否包含日期,時(shí)間或兩者。有效值是日期,時(shí)間或兩者。 如果未指定值,則使用日期。

JSF <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é)果 -

簡單寫入一些信息,然后提交 -

JSF <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é)果 -

簡單寫入一些信息,然后提交 -


上一篇:JSF JDBC連接下一篇:JSF表單組合框