Standard Converters in JSF (convertNumber and convertDateTime)

The standard converters are built-in converters in JSF Framework. These converters are usually associated with the User Interface components with the help of tags that are available at http://java.sun.com/jsf/core.

There are 2 types of Standard Converters in JSF:

  • convertNumber
  • convertDateTime

convertNumber:

Attribute
Example
Currency Code < c:convertNumber type=”currency” currencyCode=”NZD” >
Currency Symbol < c:convertNumber type=”currency” currencySymbol=”$” >
maxFractionDigits < c:convertNumber maxFractionDigits=”3″ >
minFractionDigits < c:convertNumber minFractionDigits=”1″ >
maxIntegerDigits < c:convertNumber maxIntegerDigits=”3″ >
minIntegerDigits < c:convertNumber minIntegerDigits=”3″ >
integerOnly < c:convertNumber integerOnly=”true” >
pattern < c:convertNumber pattern=”#000.00″ >
type < c:convertNumber type=”percent” > (types- type=number,percent,currency)

convertDateTime

Attribute Example
type < c:convertDateTime type=”time” >
dateStyle < c:convertDateTime dateStyle=”long” > (types-short,medium,long,full)
timeStyle < c:convertDateTime type=”time” timeStyle=”long” >
pattern < c:convertDateTime pattern=”dd-MM-yy” > (types-short,medium,long,full)

Example:
converter.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:c="http://java.sun.com/jsf/core">
    <h:head>
        <title>Converter Page</title>
    </h:head>
    <h:body>
        <table border="3">
            <tr>
                <td>Converter</td>
                <td>Original Value</td>
                <td>Converted Value</td>
            </tr>
            
            <tr>
                <td>minFractionDigits (1)</td>
                <td><h:outputText value="23.332"/></td>
                <td> <h:outputText value="23.332" >
                     <c:convertNumber minFractionDigits="1" />
                     </h:outputText> </td>
            </tr>
            
            <tr>
                <td>maxIntegerDigits (3)</td>
                <td><h:outputText value="332343"/></td>
                <td><h:outputText value="332343">
                        <c:convertNumber maxIntegerDigits="3"/>
                    </h:outputText></td>
            </tr>
            
            <tr>
                <td>Pattern (#000.00)</td>
                <td><h:outputText value="23.332"/> </td>
                <td><h:outputText value="23.332">
                    <c:convertNumber pattern="#000.00" />    
                    </h:outputText> </td>
            </tr>
            
            <tr>
                <td>Percentage (%)</td>
                <td><h:outputText value="92.9%"/></td>
                <td><h:outputText value="92.9%">
                    <c:convertNumber type="percent" />  
                    </h:outputText> </td>
            </tr>
            
            <tr>
                <td>Currency Code:</td>
                <td><h:outputText value="NZD2500"/></td>
                <td><h:outputText value="NZD2500">
                    <c:convertNumber type="currency" currencyCode="NZD" />    
                    </h:outputText> </td>
            </tr>
            
            <tr>
                <td>Currency Symbol:</td>
                <td><h:outputText value="$2500"/></td>
                <td><h:outputText value="$2500">
                    <c:convertNumber type="currency" currencySymbol="$" /> 
                    </h:outputText> </td>
            </tr>
            
            <tr>
                <td>Convert Time (HH:ss)</td>
                <td><h:outputText value="1:10:03"/></td>
                <td><h:outputText value="1:10:03">
                        <c:convertDateTime type="time" pattern="HH:mm:ss"/> 
                    </h:outputText> </td>
            </tr>
            <h:form>
            <tr>
                <td>Date Format(dd-MM-yyyy)</td>
                <td><h:inputText value="#{fdate.date}" converterMessage="Enter in the format of dd-mm-yyyy">
                        <c:convertDateTime pattern="dd-MM-yyyy"></c:convertDateTime>
                    </h:inputText></td>
                <td> <h:commandButton value="submit" action="show"/></td>
            </tr>
            </h:form>
        </table>
    </h:body>
</html>

Output:
output1

When date entered wrong,
output2

 

By Sri

One thought on “Standard Converters in JSF (convertNumber and convertDateTime)”

Leave a Reply

Your email address will not be published. Required fields are marked *