1

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 ?

bog
  • 1,323
  • 5
  • 22
  • 34
  • Have a look at `Date#before(Date)` and `Date#after(Date)`. – AlexR Jul 28 '14 at 13:34
  • but it works with two Date objects. I have one DataPicker and one Date – bog Jul 28 '14 at 14:01
  • `new Date()` constructs a `Date` containing the current date. – AlexR Jul 28 '14 at 14:31
  • ok, I got it. The point is that I don't know how to make this expression to work: DataPicker dp = new DataPicker(); Date d = new Date(); if(d == dp.getVAl())... Or somethig like this – bog Jul 29 '14 at 09:02
  • Google for 'java datepicker get date' and find [this](http://stackoverflow.com/questions/20446026/get-value-from-date-picker) among others... – AlexR Aug 05 '14 at 10:53

2 Answers2

2

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.

AlexR
  • 2,412
  • 16
  • 26
0

You can write a custom method, which will compare given dates of given date format, and return true, when current date is "older" than your date of interest, eg:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {

    public static void main(String args[]) {
        System.out.println(isDateOfInterestValid("yyyy-mm-dd", 
                "2014-08-25", "2014-08-28"));
    }

    public static boolean isDateOfInterestValid(String dateformat, 
            String currentDate, String dateOfInterest) {

        String format = dateformat;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date cd = null;  // current date
        Date doi = null; // date of interest

        try {
            cd = sdf.parse(currentDate);
            doi = sdf.parse(dateOfInterest);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long diff = cd.getTime() - doi.getTime();
        int diffDays = (int) (diff / (24 * 1000 * 60 * 60));

        if (diffDays > 0) {
            return false;
        } else {
            return true;
        }
    }
}

And in context of pure JavaFX you can get the String value of DatePicker chosen date by calling DatePicker.getValue().toString().

PS In case You have only one DatePicker object, You can use "hidden" method, which will check the current date. It can look like this:

public static String currentDate(String separator) {
    Calendar date = new GregorianCalendar();
    String day = Integer.toString(date.get(Calendar.DAY_OF_MONTH));
    String month = Integer.toString(date.get(Calendar.MONTH) + 1);
    String year = Integer.toString(date.get(Calendar.YEAR));
    if (month.length() < 2) {
        month = "0" + month;
    }
    if (day.length() < 2) {
        day = "0" + day;
    }
    String regDate = year + separator + month + separator + day;
    return regDate;
}
bluevoxel
  • 4,978
  • 11
  • 45
  • 63
  • Och on reinventing the wheel there. `chosenDate.after(new Date())` does the same and is thoroughly tested. – AlexR Jul 28 '14 at 14:32
  • Well, necessity is the mother of invention. – bluevoxel Jul 28 '14 at 14:40
  • Yeah, but Javadoc is the mother of timesavers. Even then, `SimpleDateFormat#format(new Date());` saves you the entire method `currentDate`... – AlexR Jul 28 '14 at 14:42