- 

Available since OmniFaces 1.0

Collection of EL functions for date and time: of:formatDate(), of:formatDateWithTimezone(), of:addXxx() like of:addDays(), of:xxxBetween() like of:daysBetween(), of:getMonths(), of:getShortMonths(), of:getDaysOfWeek(), of:getShortDaysOfWeek(), of:getMonth(), of:getShortMonth(), of:getDayOfWeek() and of:getShortDayOfWeek().

Historical note: before OmniFaces 3.6, these functions accepted java.util.Date and java.util.TimeZone only. Since OmniFaces 3.6, these functions also accept java.time.Temporal and java.time.ZoneId.

Feature request

If you know more useful methods/functions which you think should be added to this OmniFaces utility class so that everyone can benefit from a "standard" Faces utility library, feel free to post a feature request.

Demo

Format current date with system default timezone: 2024-11-21 00:36:38 -06:00

Format current date with GMT timezone: 2024-11-21 06:36:38

Add 3 months to today and format it with system default timezone: 2025-02-21 00:36:38 CST

Count months between the date representing "now" and the date representing "3 months later": 3 months


Chosen month number: 1 - Name: January - Short name: Jan

Chosen day of week number: 1 - Name: Monday - Short name: Mon

Get all months for the current locale:

Get all short months for the current locale:

Get all days of week for the current locale:

Get all short days of week for the current locale:

Switch locale and see month and day of week names changing:

Demo source code
<f:view locale="#{localeManager.language}">
    <p>
        Format current date with system default timezone:
        #{of:formatDate(now, 'yyyy-MM-dd HH:mm:ss z')}
    </p>
    <p>
        Format current date with GMT timezone:
        #{of:formatDateWithTimezone(now, 'yyyy-MM-dd HH:mm:ss', 'GMT')}
    </p>
    <p>
        <c:set var="threeMonthsLater" value="#{of:addMonths(now.zonedDateTime, 3)}" scope="request" />
        Add 3 months to today and format it with system default timezone: 
        #{of:formatDate(threeMonthsLater, 'yyyy-MM-dd HH:mm:ss z')}
    </p>
    <p>
        Count months between the date representing "now" and the date representing "3 months later": 
        #{of:monthsBetween(now, threeMonthsLater)} months
    </p>
    
    <hr />

    <p>
        <h:panelGroup id="month">
            Chosen month number:
            <strong>#{functionsBean.month}</strong>
            - Name:
            <strong>#{of:getMonth(functionsBean.month)}</strong>
            - Short name:
            <strong>#{of:getShortMonth(functionsBean.month)}</strong>
        </h:panelGroup>
    </p>
    <p>
        <h:panelGroup id="dayOfWeek">
            Chosen day of week number:
            <strong>#{functionsBean.dayOfWeek}</strong>
            - Name:
            <strong>#{of:getDayOfWeek(functionsBean.dayOfWeek)}</strong>
            - Short name:
            <strong>#{of:getShortDayOfWeek(functionsBean.dayOfWeek)}</strong>
        </h:panelGroup>
    </p>

    <h:form>
        <p>
            Get all months for the current locale:
            <h:selectOneMenu value="#{functionsBean.month}">
                <f:selectItems value="#{of:getMonths()}" />
                <f:ajax render=":month" />
            </h:selectOneMenu>
        </p>
        <p>
            Get all short months for the current locale:
            <h:selectOneMenu value="#{functionsBean.month}">
                <f:selectItems value="#{of:getShortMonths()}" />
                <f:ajax render=":month" />
            </h:selectOneMenu>
        </p>
        <p>
            Get all days of week for the current locale:
            <h:selectOneMenu value="#{functionsBean.dayOfWeek}">
                <f:selectItems value="#{of:getDaysOfWeek()}" />
                <f:ajax render=":dayOfWeek" />
            </h:selectOneMenu>
        </p>
        <p>
            Get all short days of week for the current locale:
            <h:selectOneMenu value="#{functionsBean.dayOfWeek}">
                <f:selectItems value="#{of:getShortDaysOfWeek()}" />
                <f:ajax render=":dayOfWeek" />
            </h:selectOneMenu>
        </p>
        <p>
            Switch locale and see month and day of week names changing:
            <h:selectOneMenu value="#{localeManager.language}">
                <f:selectItem itemValue="en" itemLabel="English" />
                <f:selectItem itemValue="es" itemLabel="Español (Spanish)" />
                <f:selectItem itemValue="fr" itemLabel="Français (French)" />
                <f:selectItem itemValue="de" itemLabel="Deutsch (German)" />
                <f:selectItem itemValue="nl" itemLabel="Nederlands (Dutch)" />
                <f:selectItem itemValue="ar" itemLabel="العربية (Arabic)" />
                <f:selectItem itemValue="he" itemLabel="עִבְרִית (Hebrew)" />
                <f:selectItem itemValue="zh" itemLabel="汉语 (Chinese)" />
                <f:ajax render=":demo" />
            </h:selectOneMenu>
        </p>
    </h:form>
</f:view>