Sunday, 14 November 2021

What is LocalDate in Java 8 ?

LocalDate class is an immutable & thread safe class. Its based on ISO-8601 calendar system.

It represents a date without any time components. Its date format often viewed in the form year-month-day {2021-11-14}.

Using this class, you can also access other data fields like :

    > day-of-year

    > day-of-week

    > week-of-year

Example:

public class LocalDateEx{

public static void main(String[] args) {
LocalDate date = LocalDate.of(2021, 4, 14);
System.out.println("Year of the given date "+ date.getYear());
System.out.println("Month of the given date "+ date.getMonth());
System.out.println("day of week of the given date "+ date.getDayOfWeek());
System.out.println("day of month of the given date "+ date.getDayOfMonth());
System.out.println("day of year of the given date "+ date.getDayOfYear());
System.out.println("day of year of the given date "+ date.getDayOfYear());
System.out.println("month of year(1 to 12) of the given date "+
                                                                                             date.getMonthValue());
        System.out.println("is given date a leap year? "+ date.isLeapYear());
System.out.println(date);
}
}


No comments:

Post a Comment