-
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. Since OmniFaces 4.7, these functions also accept java.lang.Long as epoch milli.
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.
Format current date with system default timezone: 2025-11-15 07:51:19 -06:00
Format current date with GMT timezone: 2025-11-15 13:51:19
Add 3 months to today and format it with system default timezone: 2026-02-15 07:51:19 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
<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>package org.omnifaces.showcase.functions;
import java.io.Serializable;
import jakarta.inject.Named;
import org.omnifaces.cdi.ViewScoped;
@Named
@ViewScoped
public class FunctionsBean implements Serializable {
private static final long serialVersionUID = 1L;
private int day = 1;
private int year = 2003;
private int month = 1;
private int dayOfWeek = 1;
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDayOfWeek() {
return dayOfWeek;
}
public void setDayOfWeek(int dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
}package org.omnifaces.showcase.functions;
import java.io.Serializable;
import java.util.Locale;
import jakarta.enterprise.context.SessionScoped;
import jakarta.inject.Named;
import org.omnifaces.util.Faces;
@Named
@SessionScoped
public class LocaleManager implements Serializable {
private static final long serialVersionUID = 1L;
private Locale locale = Faces.getLocale();
public Locale getLocale() {
return locale;
}
public String getLanguage() {
return locale.getLanguage();
}
public void setLanguage(String language) {
locale = new Locale(language);
Faces.setLocale(locale);
}
}VDL documentation
of:formatDate.fnof:formatDateWithTimezone.fnof:addYears.fnof:addMonths.fnof:addWeeks.fnof:addDays.fnof:addHours.fnof:addMinutes.fnof:addSeconds.fnof:yearsBetween.fnof:monthsBetween.fnof:weeksBetween.fnof:daysBetween.fnof:hoursBetween.fnof:minutesBetween.fnof:secondsBetween.fnof:getMonths.fnof:getShortMonths.fnof:getDaysOfWeek.fnof:getShortDaysOfWeek.fnof:getMonth.fnof:getShortMonth.fnof:getDayOfWeek.fnof:getShortDayOfWeek.fn