PrimeFaces - Date Range Picker with second selectable date always starts from the first one

[Updated: Jun 15, 2017, Created: Jun 14, 2017]

In the last example, we saw how to implement a date picker. We implemented a custom JSF Validator to restrict the user to select the second date to be more than or equal to the first date. In this example, we will show how to restrict second calendar's minimum value to be always more than the first calendar selected date. In this case, we will need no date validations.

JSF page


    <h2>Primefaces Date Range Picker example</h2>
    <p:outputPanel id="mainPanel">
        <p:outputLabel for="dateSelectBtn" value="#{dateRange.dateRangeString}"/>

    <p:commandButton id="dateSelectBtn" value="Select new date range" type="button"
    <p:dialog closable="false"
              header="Date Range Picker" resizable="false"
              widgetVar="dateRangeDlg" minHeight="40" modal="true">
                <p:calendar id="idStartDate" readonlyInput="true"
                    <p:ajax event="dateSelect" update="idEndDate"/>
                <p:calendar id="idEndDate" readonlyInput="true"
                    <p:ajax event="dateSelect"/>
                <p:commandButton value="Close" icon="ui-icon-close"

The manage bean

@ManagedBean(name = "dateRange")
public class DateRangeBean {
  private Date startDate;
  private Date endDate;
  private DateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");

  public DateRangeBean() {
      Calendar c1 = Calendar.getInstance();
      endDate = c1.getTime();
      Calendar c2 = Calendar.getInstance();
      c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
      c2.set(Calendar.DAY_OF_YEAR, 1);
      startDate = c2.getTime();

  public Date getStartDate() {
      return startDate;

  public void setStartDate(Date startDate) {
      this.startDate = startDate;
          endDate = startDate;

  public Date getEndDate() {
      return endDate;

  public void setEndDate(Date endDate) {
      this.endDate = endDate;

  public String getDateRangeString() {
      return String.format("From: %s To: %s%n",
              formatter.format(startDate), formatter.format(endDate));

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


Example Project

Dependencies and Technologies Used:

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Date Range Picker With End Date Restriction Select All Download
  • date-range-picker-with-min-date-restriction
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • webapp

See Also