-
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.
Format current date with system default timezone: 2024-12-21 03:41:35 -06:00
Format current date with GMT timezone: 2024-12-21 09:41:35
Add 3 months to today and format it with system default timezone: 2025-03-21 03:41:35 CDT
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.fn
of:formatDateWithTimezone.fn
of:addYears.fn
of:addMonths.fn
of:addWeeks.fn
of:addDays.fn
of:addHours.fn
of:addMinutes.fn
of:addSeconds.fn
of:yearsBetween.fn
of:monthsBetween.fn
of:weeksBetween.fn
of:daysBetween.fn
of:hoursBetween.fn
of:minutesBetween.fn
of:secondsBetween.fn
of:getMonths.fn
of:getShortMonths.fn
of:getDaysOfWeek.fn
of:getShortDaysOfWeek.fn
of:getMonth.fn
of:getShortMonth.fn
of:getDayOfWeek.fn
of:getShortDayOfWeek.fn