Decom1
Decom1

Reputation: 3

Javafx Using Date Picker

I am creating a Java Fx Application using Scene Builder. I have two Date Pickers Date1 and Date Two. I need to count the number of days between Date1 and Date2 excluding any Sunday. I have searched various answers but none that caters.

Any help much appreciated.

Upvotes: 0

Views: 2534

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48751

The following should work.

long date1 = datePicker1.getvalue().toEpochDay();
long date2 = datePicker2.getvalue().toEpochDay();
int  days  = (int) Math.abs(date1 - date2);

Example:

long date1 = 16322; // 09/09/2014
long date2 = 16329; // 09/16/2014
int  days  = (int) Math.abs(date1 - date2);

System.out.println(days); // 7 Days

Note: I do not think jfx2.0 has a built-in DatePicker, so I am assuming you are using jdk8.

Also, I pulled the the datePicker.getvalue().toEpochDay() logic from this question:
Stack Overflow: Get value from Date picker; which deals with jfx8.

The epoch in the LocalDate.toEpochDay() is the number of days since 01/01/1970.


Extra Credit

To Answer you question from the comment below, you could do the following.

int days = daysBetween(
    datePicker1.getvalue(),
    datePicker2.getvalue(),
    Arrays.asList(DayOfWeek.SUNDAY)
);

public static int daysBetween(LocalDate start, LocalDate end, List<DayOfWeek> ignore) {
    int count = 0;
    LocalDate curr = start.plusDays(0); // Create copy.
    while (curr.isBefore(end)) {
        if (!ignore.contains(curr.getDayOfWeek()))
            count++;
        curr = curr.plusDays(1); // Increment by a day.
    }
    return count;
}

Upvotes: 1

Related Questions