cssjavafxrowlocaldate

How to compare expiry date to today date?


I'm trying to compare two dates using LocalDate in my Expire date table. The problem is that I cannot set properly row formatting according to dateExpiry.

This is how it should look: How to set properly cell formating

if (today.isEqual(LocalDate.parse(medicament))) {
    currentRow.setStyle("-fx-background-color:tomato");
} else if (today.before(LocalDate.parse(medicament).plusMonths(3))) {
    currentRow.setStyle("-fx-background-color:orange");
} else {
    currentRow.setStyle("-fx-background-color:lightgreen");
}

Solution

  • import java.time.LocalDate;
    
        public class ExpireDateTable {
            public static void main(String[] args) {
                // Sample dates
                LocalDate currentDate = LocalDate.now();
                LocalDate dateExpiry = LocalDate.of(2024, 2, 28); //Example expiry date
    
                // Comparing dates and applying row formatting
                if (dateExpiry.isBefore(currentDate)) {
                    System.out.println("Row formatting: Date expired, apply formatting for past expiry date.");
                    // Apply formatting for past expiry date
                } else if (dateExpiry.isEqual(currentDate)) {
                    System.out.println("Row formatting: Date expiring today, apply formatting accordingly.");
                    // Apply formatting for expiry date today
                } else {
                    System.out.println("Row formatting: Date is in the future, no special formatting needed.");
                    // Apply default formatting
                }
            }
        }