javafx: how to check if a date is greater than today's date?

bog picture bog · Jul 28, 2014 · Viewed 9.7k times · Source

If I have:

DatePicker dp = new DataPicker();

and at some point I want to know if the data is greater than today, how can I do it?

Example: if I want to book a room in a hotel from 21/04/2014 well, it should be impossible because today is 28/07/2014.

How can I do it in JavaFX ?

Answer

AlexR picture AlexR · Jul 28, 2014

To ensure that a given Date chosenDate is after today, you can check

if (chosenDate.after(new Date())) {
    // valid (Date > today)
} else {
    // invalid (Date <= today)
}

Note that chosenDate should be a Date with hour, minute and second set to 0 since else it could accept a Date with the same day as today but a later hour than now.