Java Data Type How to - Check Is leap year, is before for a date








Question

We would like to know how to check Is leap year, is before for a date.

Answer

import java.time.LocalDate;
import java.time.LocalTime;
/*from w  w  w.j  a  v a2s  . co m*/
public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        // Get the Year, check if it's leap year
        System.out.println("Year " + today.getYear() + " is Leap Year? "
                + today.isLeapYear());

        // Compare two LocalDate for before and after
        System.out.println("Today is before 01/01/2015? "
                + today.isBefore(LocalDate.of(2015, 1, 1)));

        // Create LocalDateTime from LocalDate
        System.out.println("Current Time=" + today.atTime(LocalTime.now()));

    }
}

The code above generates the following result.