Finally, with JavaFX 8 a DatePicker control was added (for JavaFX 2 we had to create our own)!

The DatePicker works well in combination with the new Java 8 Date and Time API. Those two things together provide a much better experience when working with dates in Java!

JavaFX Date Picker

Basic Usage

Using the DatePicker is straight forward:

// Create the DatePicker.
DatePicker datePicker = new DatePicker();

// Add some action (in Java 8 lambda syntax style).
datePicker.setOnAction(event -> {
    LocalDate date = datePicker.getValue();
    System.out.println("Selected date: " + date);
});

// Add the DatePicker to the Stage.
StackPane root = new StackPane();
root.getChildren().add(datePicker);
stage.setScene(new Scene(root, 500, 650));
stage.show();

Using FXML

I usually prefer keeping as much of the view in fxml instead of instantiating the controls in Java code as above. With Scene Builder 2.0 and above you can even drag-and-drop the DatePicker into your fxml.

Options

Hide Week Numbers

We can set the showWeekNumbersProperty to true/false to show/hide a column showing week numbers. Note: The default value depends on the country of the current locale.

JavaFX Date Picker No Weeks

datePicker.setShowWeekNumbers(false);

Date Converter

The date in the text field is automatically converted to the local format (in my case, it's dd.MM.yyyy). By setting a StringConverter we can change this, for example, to yyyy-MM-dd.

JavaFX Date Picker Converter

String pattern = "yyyy-MM-dd";

datePicker.setPromptText(pattern.toLowerCase());

datePicker.setConverter(new StringConverter<LocalDate>() {
     DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);

     @Override 
     public String toString(LocalDate date) {
         if (date != null) {
             return dateFormatter.format(date);
         } else {
             return "";
         }
     }

     @Override 
     public LocalDate fromString(String string) {
         if (string != null && !string.isEmpty()) {
             return LocalDate.parse(string, dateFormatter);
         } else {
             return null;
         }
     }
 });

Other Calendar Systems

If you need a different chronology, e.g. the Japanese Imperial calendar system, you can change it as follows:

// Japanese calendar.
datePicker.setChronology(JapaneseChronology.INSTANCE);

// Hijrah calendar.
datePicker.setChronology(HijrahChronology.INSTANCE);

// Minguo calendar.
datePicker.setChronology(MinguoChronology.INSTANCE);

// Buddhist calendar.
datePicker.setChronology(ThaiBuddhistChronology.INSTANCE);

Here is a screenshot of the Hijrah calendar:

Hijrah Calendar