f:convertNumberタグとf:convertDateタグ
【概要】
桁数の指定やパーセント表示などができます。
SimpleDateForamtと同じような動作をします。
【サンプルコード】
まずはManagedBeanからです。
ManagedBeanはプロパティに適当な値を設定するのみです。
@ManagedBean(name = "converterSample") @RequestScoped public class ConverterSample { private Integer intValue = null; private Double doubleValue = null; private Timestamp timestampValue = null; private Date dateValue = null; public ConverterSample() { intValue = 1000; doubleValue = 1000.123d; Calendar cal = Calendar.getInstance(); timestampValue = new Timestamp(cal.getTimeInMillis()); dateValue = cal.getTime(); } }
つづいて、ページです。
ManagedBeanのプロパティ値を表示しています。
<?xml version="1.0" encoding="Windows-31J" ?> <!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"> <f:view> <h:head> <meta http-equiv="Content-Type" content="text/html; charset=Windows-31J" /> <title>コンバートサンプル</title> </h:head> <h:body> <div style="background-color: #FFDDDD; margin-bottom: 10px;"> <h4>Integer変換</h4> <div> <span>通常出力 ⇒ </span> <h:outputText value="#{converterSample.intValue}" /> </div> <div> <span>パターン指定 ⇒ </span> <h:outputText value="#{converterSample.intValue}" > <f:convertNumber type="number" pattern="###,000,000"/> </h:outputText> </div> <div> <span>通貨マーク指定 ⇒ </span> <h:outputText value="#{converterSample.intValue}" > <f:convertNumber type="currency" currencySymbol="\"/> </h:outputText> </div> <div> <span>パーセント表示 ⇒ </span> <h:outputText value="#{converterSample.intValue}" > <f:convertNumber type="percent"/> </h:outputText> </div> </div> <div style="background-color: #DDFFDD; margin-bottom: 10px;"> <h4>Dobule変換</h4> <div> <span>通常出力 ⇒ </span> <h:outputText value="#{converterSample.doubleValue}" /> </div> <div> <span>表示桁数指定 ⇒ </span> <h:outputText value="#{converterSample.doubleValue}" > <f:convertNumber minIntegerDigits="5" minFractionDigits="4"/> </h:outputText> </div> </div> <div style="background-color: #DDDDFF; margin-bottom: 10px;"> <h4>Date変換</h4> <div> <span>通常出力 ⇒ </span> <h:outputText value="#{converterSample.dateValue}" /> </div> <div> <span>パターン指定1 ⇒ </span> <h:outputText value="#{converterSample.dateValue}" > <f:convertDateTime pattern="yyyy/MM/dd hh:mm:ss"/> </h:outputText> </div> <div> <span>パターン指定2 ⇒ </span> <h:outputText value="#{converterSample.dateValue}" > <f:convertDateTime pattern="yyyy/MM/dd HH:mm:ss" timeZone="JST"/> </h:outputText> </div> </div> <div style="background-color: #FFDDFF; margin-bottom: 10px;"> <h4>Timestamp変換</h4> <div> <span>通常出力 ⇒ </span> <h:outputText value="#{converterSample.timestampValue}" /> </div> <div> <span>パターン指定1 ⇒ </span> <h:outputText value="#{converterSample.timestampValue}" > <f:convertDateTime pattern="yyyy/MM/dd HH:mm:ss.SSS"/> </h:outputText> </div> <div> <span>パターン指定2 ⇒ </span> <h:outputText value="#{converterSample.timestampValue}" > <f:convertDateTime pattern="yyyy/MM/dd HH:mm:ss.SSS" timeZone="JST"/> </h:outputText> </div> </div> </h:body> </f:view> </html>
【解説】
type属性には数値を表す「number」、通過を表す「currency」、パーセンテージを表す「percent」の
いずれかを指定します。
デフォルト値は「number」です。
pattern属性には表示のパターンを指定します。
サンプルでは「###,000,000」と指定したので、「1000」である「intValue」は
「001,000」と表示されています。
currencySymbol属性には通貨マークを指定します。
サンプルでは「\」としてしたので、「\」が値の前に付け足されています。
minIntegerDigits属性は整数の表示桁数を、
minFractionDigits属性は小数の表示桁数を指定します。
maxIntegerDigits属性、maxFractionDigits属性もあります。
pattern属性には日付の表示形式を指定します。
timeZone属性の指定も必要になります。
timeZone属性を指定していない場合、グリニッジ標準時間が適用されるようです。
【最後に】
数値・日付の表示形式の指定が簡単にできます。
覚えておいて損はないと思います。