DatePicker has first class support for Java 8+ Date APIs such as LocalDate.
<h:outputScript name="locales/locale-de.js" library="primefaces"/>
<h:outputScript name="locales/locale-es.js" library="primefaces"/>
<script>
//<![CDATA[
function dateTemplateFunc(date) {
return '<span style="background-color:' + ((date.day < 21 && date.day > 10) ? '#81C784' : 'inherit') + ';border-radius:50%;width: 2.5rem;height: 2.5rem;line-height: 2.5rem;display: flex;align-items: center;justify-content: center;">' + date.day + '</span>';
}
//]]>
</script>
<h:form id="form">
<div class="card">
<p:growl id="msgs" showDetail="true" skipDetailIfEqualsSummary="true">
<p:autoUpdate/>
</p:growl>
<h5 class="mt-0">Popup</h5>
<div class="ui-fluid grid formgrid">
<div class="field col-12 md:col-4">
<p:outputLabel for="basic" value="Basic"/>
<p:datePicker id="basic" value="#{calendarJava8View.date}"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="popupConvertDateTime" value="Popup (f:convertDateTime)"/>
<p:datePicker id="popupConvertDateTime" value="#{calendarJava8View.date13}"
pattern="MM/dd/yyyy">
<f:convertDateTime pattern="MM/dd/yyyy" type="localDate"/>
</p:datePicker>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="multiselect" value="Multiple Selection"/>
<p:datePicker id="multiselect" selectionMode="multiple"
value="#{calendarJava8View.multi}"
readonlyInput="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="range" value="Range Selection"/>
<p:datePicker id="range" selectionMode="range" value="#{calendarJava8View.range}"
readonlyInput="true" hideOnRangeSelection="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="navigator" value="Navigator"/>
<p:datePicker id="navigator" value="#{calendarJava8View.date2}"
monthNavigator="true"
yearNavigator="true"
showWeek="true" showIcon="true" showOnFocus="false"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="@next" value="Week Picker"/>
<p:datePicker id="week" view="week" value="#{calendarJava8View.week}"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="month" value="Month Picker"/>
<p:datePicker id="month" view="month" value="#{calendarJava8View.date3}"
pattern="MM/yyyy"
yearNavigator="true" yearRange="2000:2030"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="yearMonth" value="Month Picker (java.time.YearMonth)"/>
<p:datePicker id="yearMonth" view="month" value="#{calendarJava8View.yearMonth}"
pattern="MM/yyyy" yearNavigator="true" yearRange="2000:2030"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="touch" value="Touch UI"/>
<p:datePicker id="touch" value="#{calendarJava8View.date4}" touchUI="true"
autoDetectDisplay="false"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="mask" value="Mask"/>
<p:datePicker id="mask" value="#{calendarJava8View.date14}" pattern="MM/dd/yyyy"
showButtonBar="true" mask="true" showOnFocus="false"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="dateTemplate" value="Date Template"/>
<p:datePicker id="dateTemplate" value="#{calendarJava8View.date5}"
dateTemplate="dateTemplateFunc"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="disabledDD" value="Disabled Date/Day"/>
<p:datePicker id="disabledDD" value="#{calendarJava8View.date6}"
disabledDays="#{calendarJava8View.invalidDays}"
disabledDates="#{calendarJava8View.invalidDates}" readonlyInput="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="button" value="Button"/>
<p:datePicker id="button" value="#{calendarJava8View.date7}" showIcon="true"
showOnFocus="false"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="event" value="Select Event"/>
<p:datePicker id="event" value="#{calendarJava8View.date8}">
<p:ajax event="dateSelect" listener="#{calendarJava8View.onDateSelect}"
update="msgs"/>
</p:datePicker>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="spanish" value="Spanish"/>
<p:datePicker id="spanish" value="#{calendarJava8View.date9}" locale="es"
monthNavigator="true" pattern="yyyy-MMM-dd"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="german" value="German"/>
<p:datePicker id="german" widgetVar="german" value="#{calendarJava8View.dateDe}"
locale="de" monthNavigator="true" pattern="dd.MM.yyyy"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="buttonbar" value="Button Bar"/>
<p:datePicker id="buttonbar" value="#{calendarJava8View.date10}" showButtonBar="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="minmax" value="Min-Max"/>
<p:datePicker id="minmax" value="#{calendarJava8View.date11}"
mindate="#{calendarJava8View.minDate}"
maxdate="#{calendarJava8View.maxDate}"
readonlyInput="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="multi" value="Multiple Months"/>
<p:datePicker id="multi" value="#{calendarJava8View.date12}" numberOfMonths="3"
monthNavigator="true" yearNavigator="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="enabledDates" value="Enabled Dates"/>
<p:datePicker id="enabledDates" value="#{calendarJava8View.date15}"
enabledDates="#{calendarJava8View.validDates}"
readonlyInput="true"/>
</div>
</div>
<h5>Inline</h5>
<p:datePicker id="inline" value="#{calendarJava8View.date1}" inline="true"/>
</div>
<div class="card">
<h5>DateTime</h5>
<div class="ui-fluid grid formgrid">
<div class="field col-12 md:col-4">
<p:outputLabel for="time1" value="Time (with Select Event)"/>
<p:datePicker id="time1" value="#{calendarJava8View.dateTime1}" showButtonBar="true">
<p:ajax event="dateSelect" listener="#{calendarJava8View.onDateTimeSelect}"
update="msgs"/>
</p:datePicker>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="time3" value="Time (Min-Max)"/>
<p:datePicker id="time3" value="#{calendarJava8View.dateTime3}"
mindate="#{calendarJava8View.minDateTime}"
maxdate="#{calendarJava8View.maxDateTime}"
showButtonBar="true"
/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="time4" value="Time (Min-Max; preset value)"/>
<p:datePicker id="time4" value="#{calendarJava8View.dateTime4}"
mindate="#{calendarJava8View.minDateTime}"
maxdate="#{calendarJava8View.maxDateTime}"
/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="time5" value="Time (with seconds)"/>
<p:datePicker id="time5" value="#{calendarJava8View.dateTime5}"
showSeconds="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeDe" value="Time (German)"/>
<p:datePicker id="timeDe" value="#{calendarJava8View.dateTimeDe}"
locale="de" pattern="dd.MM.yyyy"
/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="time2" value="Time (Timezone GMT+8)"/>
<p:datePicker id="time2" value="#{calendarJava8View.dateTime2}"
timeZone="GMT+8"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="zonedDateTime1" value="Time (ZonedDateTime; not supported yet)"/>
<p:datePicker id="zonedDateTime1" value="#{calendarJava8View.zonedDateTime1}"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="time7" value="Time (with timeInput)"/>
<p:datePicker id="time7" value="#{calendarJava8View.dateTime7}" timeInput="true"/>
</div>
</div>
</div>
<div class="card">
<h5>Time Only</h5>
<div class="ui-fluid grid formgrid">
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonly" value="Time Only (HH:mm)"/>
<p:datePicker id="timeonly" value="#{calendarJava8View.time1}"
pattern="HH:mm" showButtonBar="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonlyMinMax" value="Time Only (Min-Max)"/>
<p:outputPanel>
<p:datePicker id="timeonlyMinMax" value="#{calendarJava8View.time3}"
mindate="#{calendarJava8View.minTime}"
maxdate="#{calendarJava8View.maxTime}"
/>
(between <h:outputText value="#{calendarJava8View.minTime}"></h:outputText> and
<h:outputText value="#{calendarJava8View.maxTime}"></h:outputText>)
</p:outputPanel>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonlyMinMax2" value="Time Only (Min-Max; preset value)"/>
<p:outputPanel>
<p:datePicker id="timeonlyMinMax2" value="#{calendarJava8View.time4}"
mindate="#{calendarJava8View.minTime}"
maxdate="#{calendarJava8View.maxTime}"
/>
(between <h:outputText value="#{calendarJava8View.minTime}"></h:outputText> and
<h:outputText value="#{calendarJava8View.maxTime}"></h:outputText>)
</p:outputPanel>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonlySeconds" value="Time Only (HH:mm:ss)"/>
<p:datePicker id="timeonlySeconds" value="#{calendarJava8View.time5}"
showSeconds="true" pattern="HH:mm:ss"
/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonly12" value="Time Only (AM/PM)"/>
<p:datePicker id="timeonly12" value="#{calendarJava8View.time6}"
hourFormat="12" pattern="HH:mm"
/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonly8" value="Time Only (with timeInput)"/>
<p:datePicker id="timeonly8" value="#{calendarJava8View.time8}" timeInput="true"/>
</div>
<div class="field col-12 md:col-4">
<p:outputLabel for="timeonlyMilliseconds" value="Time Only (HH:mm:ss.SSS a)"/>
<p:datePicker id="timeonlyMilliseconds" value="#{calendarJava8View.time9}"
showSeconds="true" showMilliseconds="true" pattern="HH:mm:ss.SSS a"
hourFormat="12"
showButtonBar="true"/>
</div>
</div>
</div>
</h:form>
package org.primefaces.showcase.view.input;
import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import jakarta.validation.constraints.Future;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.PrimeFaces;
import org.primefaces.event.SelectEvent;
@Named
@ViewScoped
@RegisterForReflection(serialization = true)
public class CalendarJava8View implements Serializable {
private LocalDate date;
private LocalDate date1;
@Future
private LocalDate date2;
private LocalDate date3;
private LocalDate date4;
private LocalDate date5;
private LocalDate date6;
private LocalDate date7;
private LocalDate date8;
private LocalDate date9;
private LocalDate dateDe;
private LocalDate date10;
private LocalDate date11;
private LocalDate date12;
private LocalDate date13;
private LocalDate date14;
private LocalDate date15;
private LocalTime time1;
private LocalTime time2;
private LocalTime time3;
private LocalTime time4;
private LocalTime time5;
private LocalTime time6;
private LocalTime time7;
private LocalTime time8;
private LocalTime time9;
private LocalDateTime dateTime1;
@Future
private LocalDateTime dateTime2;
private LocalDateTime dateTime3;
private LocalDateTime dateTime4;
private LocalDateTime dateTime5;
private LocalDateTime dateTime6;
private LocalDateTime dateTime7;
private LocalDateTime dateTimeDe;
private List<LocalDate> multi;
private List<LocalDate> week;
private List<LocalDate> range;
private List<LocalDate> invalidDates;
private List<LocalDate> validDates;
private List<Integer> invalidDays;
private LocalDate minDate;
private LocalDate maxDate;
private LocalTime minTime;
private LocalTime maxTime;
private LocalDateTime minDateTime;
private LocalDateTime maxDateTime;
private ZonedDateTime zonedDateTime1;
private YearMonth yearMonth;
@PostConstruct
public void init() {
invalidDates = new ArrayList<>();
invalidDates.add(LocalDate.now());
for (int i = 0; i < 5; i++) {
invalidDates.add(invalidDates.get(i).plusDays(1));
}
validDates = new ArrayList<>();
validDates.add(LocalDate.now());
for (int i = 0; i < 5; i++) {
validDates.add(validDates.get(i).plusDays(1));
}
invalidDays = new ArrayList<>();
invalidDays.add(0);
/* the first day of week is disabled */
invalidDays.add(3);
minDate = LocalDate.now().minusYears(1);
maxDate = LocalDate.now().plusYears(1);
minTime = LocalTime.of(9, 0);
maxTime = LocalTime.of(17, 0);
minDateTime = LocalDateTime.now().minusWeeks(1);
maxDateTime = LocalDateTime.now().plusWeeks(1);
dateDe = LocalDate.of(2019, 7, 27);
dateTimeDe = LocalDateTime.of(2019, 7, 27, 12, 59);
dateTime4 = LocalDateTime.now();
time4 = LocalTime.of(10, 30);
}
public void onDateSelect(SelectEvent<LocalDate> event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", event.getObject().format(formatter)));
}
public void onDateTimeSelect(SelectEvent<LocalDateTime> event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", event.getObject().format(formatter)));
}
public void click() {
PrimeFaces.current().ajax().update("form:display");
PrimeFaces.current().executeScript("PF('dlg').show()");
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public LocalDate getDate1() {
return date1;
}
public void setDate1(LocalDate date1) {
this.date1 = date1;
}
public LocalDate getDate2() {
return date2;
}
public void setDate2(LocalDate date2) {
this.date2 = date2;
}
public LocalDate getDate3() {
return date3;
}
public void setDate3(LocalDate date3) {
this.date3 = date3;
}
public LocalDate getDate4() {
return date4;
}
public void setDate4(LocalDate date4) {
this.date4 = date4;
}
public LocalDate getDate5() {
return date5;
}
public void setDate5(LocalDate date5) {
this.date5 = date5;
}
public LocalDate getDate6() {
return date6;
}
public void setDate6(LocalDate date6) {
this.date6 = date6;
}
public LocalDate getDate7() {
return date7;
}
public void setDate7(LocalDate date7) {
this.date7 = date7;
}
public LocalDate getDate8() {
return date8;
}
public void setDate8(LocalDate date8) {
this.date8 = date8;
}
public LocalDate getDate9() {
return date9;
}
public void setDate9(LocalDate date9) {
this.date9 = date9;
}
public LocalDate getDateDe() {
return dateDe;
}
public void setDateDe(LocalDate dateDe) {
this.dateDe = dateDe;
}
public LocalDate getDate10() {
return date10;
}
public void setDate10(LocalDate date10) {
this.date10 = date10;
}
public LocalDate getDate11() {
return date11;
}
public void setDate11(LocalDate date11) {
this.date11 = date11;
}
public LocalDate getDate12() {
return date12;
}
public void setDate12(LocalDate date12) {
this.date12 = date12;
}
public LocalDateTime getDateTime1() {
return dateTime1;
}
public void setDateTime1(LocalDateTime dateTime1) {
this.dateTime1 = dateTime1;
}
public LocalTime getTime1() {
return time1;
}
public void setTime1(LocalTime time1) {
this.time1 = time1;
}
public List<LocalDate> getMulti() {
return multi;
}
public void setMulti(List<LocalDate> multi) {
this.multi = multi;
}
public List<LocalDate> getRange() {
return range;
}
public void setRange(List<LocalDate> range) {
this.range = range;
}
public List<LocalDate> getInvalidDates() {
return invalidDates;
}
public void setInvalidDates(List<LocalDate> invalidDates) {
this.invalidDates = invalidDates;
}
public List<LocalDate> getValidDates() {
return validDates;
}
public void setValidDates(List<LocalDate> validDates) {
this.validDates = validDates;
}
public List<Integer> getInvalidDays() {
return invalidDays;
}
public void setInvalidDays(List<Integer> invalidDays) {
this.invalidDays = invalidDays;
}
public LocalDate getMinDate() {
return minDate;
}
public void setMinDate(LocalDate minDate) {
this.minDate = minDate;
}
public LocalDate getMaxDate() {
return maxDate;
}
public void setMaxDate(LocalDate maxDate) {
this.maxDate = maxDate;
}
public LocalDateTime getDateTimeDe() {
return dateTimeDe;
}
public void setDateTimeDe(LocalDateTime dateTimeDe) {
this.dateTimeDe = dateTimeDe;
}
public LocalTime getTime2() {
return time2;
}
public void setTime2(LocalTime time2) {
this.time2 = time2;
}
public LocalTime getTime3() {
return time3;
}
public void setTime3(LocalTime time3) {
this.time3 = time3;
}
public LocalDate getDate13() {
return date13;
}
public void setDate13(LocalDate date13) {
this.date13 = date13;
}
public LocalDate getDate14() {
return date14;
}
public void setDate14(LocalDate date14) {
this.date14 = date14;
}
public LocalDate getDate15() {
return date15;
}
public void setDate15(LocalDate date15) {
this.date15 = date15;
}
public ZonedDateTime getZonedDateTime1() {
return zonedDateTime1;
}
public void setZonedDateTime1(ZonedDateTime zonedDateTime1) {
this.zonedDateTime1 = zonedDateTime1;
}
public LocalDateTime getDateTime2() {
return dateTime2;
}
public void setDateTime2(LocalDateTime dateTime2) {
this.dateTime2 = dateTime2;
}
public YearMonth getYearMonth() {
return yearMonth;
}
public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}
public LocalTime getMinTime() {
return minTime;
}
public void setMinTime(LocalTime minTime) {
this.minTime = minTime;
}
public LocalTime getMaxTime() {
return maxTime;
}
public void setMaxTime(LocalTime maxTime) {
this.maxTime = maxTime;
}
public LocalDateTime getMinDateTime() {
return minDateTime;
}
public void setMinDateTime(LocalDateTime minDateTime) {
this.minDateTime = minDateTime;
}
public LocalDateTime getMaxDateTime() {
return maxDateTime;
}
public void setMaxDateTime(LocalDateTime maxDateTime) {
this.maxDateTime = maxDateTime;
}
public LocalDateTime getDateTime3() {
return dateTime3;
}
public void setDateTime3(LocalDateTime dateTime3) {
this.dateTime3 = dateTime3;
}
public LocalTime getTime4() {
return time4;
}
public void setTime4(LocalTime time4) {
this.time4 = time4;
}
public LocalTime getTime5() {
return time5;
}
public void setTime5(LocalTime time5) {
this.time5 = time5;
}
public LocalTime getTime6() {
return time6;
}
public void setTime6(LocalTime time6) {
this.time6 = time6;
}
public LocalDateTime getDateTime4() {
return dateTime4;
}
public void setDateTime4(LocalDateTime dateTime4) {
this.dateTime4 = dateTime4;
}
public LocalDateTime getDateTime5() {
return dateTime5;
}
public void setDateTime5(LocalDateTime dateTime5) {
this.dateTime5 = dateTime5;
}
public LocalDateTime getDateTime6() {
return dateTime6;
}
public void setDateTime6(LocalDateTime dateTime6) {
this.dateTime6 = dateTime6;
}
public LocalTime getTime7() {
return time7;
}
public void setTime7(LocalTime time7) {
this.time7 = time7;
}
public LocalDateTime getDateTime7() {
return dateTime7;
}
public void setDateTime7(LocalDateTime dateTime7) {
this.dateTime7 = dateTime7;
}
public LocalTime getTime8() {
return time8;
}
public void setTime8(LocalTime time8) {
this.time8 = time8;
}
public LocalTime getTime9() {
return time9;
}
public void setTime9(LocalTime time9) {
this.time9 = time9;
}
public List<LocalDate> getWeek() {
return week;
}
public void setWeek(List<LocalDate> week) {
this.week = week;
}
}