Java LocalDate class

Java LocalDate is a class in the Java 8 Date-Time API that represents a date (year, month, day) without a time zone. It provides several methods to manipulate and format dates.

Here’s an example of how to create a LocalDate object:

LocalDate today = LocalDate.now(); // Creates a LocalDate object representing today's date
LocalDate specificDate = LocalDate.of(2022, 5, 12); // Creates a LocalDate object representing May 12, 2022

You can also use methods to get or set specific values of a LocalDate object:

int year = today.getYear(); // Gets the year of a LocalDate object
int month = today.getMonthValue(); // Gets the month of a LocalDate object as an integer
String monthName = today.getMonth().name(); // Gets the name of the month of a LocalDate object
int day = today.getDayOfMonth(); // Gets the day of the month of a LocalDate object

You can perform arithmetic operations on a LocalDate object to add or subtract time periods:

LocalDate tomorrow = today.plusDays(1); // Adds one day to today's date
LocalDate nextMonth = today.plusMonths(1); // Adds one month to today's date
LocalDate nextYear = today.plusYears(1); // Adds one year to today's date

Overall, the LocalDate class provides a simple and convenient way to work with dates in Java.

Java LocalDate class declaration:

The LocalDate class is part of the java.time package in Java 8 and later versions. Its full declaration is as follows:

public final class LocalDate
    implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
    
    // Constructors
    private LocalDate(int year, int month, int dayOfMonth);
    private LocalDate(int year, Month month, int dayOfMonth);
    public static LocalDate now();
    public static LocalDate now(ZoneId zone);
    public static LocalDate now(Clock clock);
    public static LocalDate of(int year, int month, int dayOfMonth);
    public static LocalDate of(int year, Month month, int dayOfMonth);
    public static LocalDate ofYearDay(int year, int dayOfYear);
    public static LocalDate ofEpochDay(long epochDay);
    
    // Instance methods
    public int getYear();
    public int getMonthValue();
    public Month getMonth();
    public int getDayOfMonth();
    public int getDayOfYear();
    public DayOfWeek getDayOfWeek();
    public boolean isLeapYear();
    public LocalDate withYear(int year);
    public LocalDate withMonth(int month);
    public LocalDate withDayOfMonth(int dayOfMonth);
    public LocalDate withDayOfYear(int dayOfYear);
    public LocalDate plusYears(long yearsToAdd);
    public LocalDate plusMonths(long monthsToAdd);
    public LocalDate plusWeeks(long weeksToAdd);
    public LocalDate plusDays(long daysToAdd);
    public LocalDate minusYears(long yearsToSubtract);
    public LocalDate minusMonths(long monthsToSubtract);
    public LocalDate minusWeeks(long weeksToSubtract);
    public LocalDate minusDays(long daysToSubtract);
    public LocalDate plus(TemporalAmount amountToAdd);
    public LocalDate minus(TemporalAmount amountToSubtract);
    public long until(Temporal endExclusive, TemporalUnit unit);
    public boolean isAfter(ChronoLocalDate other);
    public boolean isBefore(ChronoLocalDate other);
    public boolean isEqual(ChronoLocalDate other);
    public boolean equals(Object obj);
    public int hashCode();
    public String toString();
    public String format(DateTimeFormatter formatter);
    public ChronoLocalDateTime<LocalDate> atTime(LocalTime time);
    public LocalDateTime atTime(int hour, int minute);
    public LocalDateTime atTime(int hour, int minute, int second);
    public LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond);
    public ZonedDateTime atStartOfDay(ZoneId zone);
    public long toEpochDay();
    public static LocalDate from(TemporalAccessor temporal);
}

As can be seen from the declaration, LocalDate implements several interfaces, including Temporal, TemporalAdjuster, ChronoLocalDate, and Serializable. The class provides a variety of constructors and methods for working with dates.

Methods of Java LocalDate:

Java LocalDate is a class that represents a date (year, month, day) without a time-zone in the ISO-8601 calendar system. Here are some methods of the LocalDate class in Java:

  1. now(): This method returns the current date based on the system clock.
  2. of(int year, int month, int dayOfMonth): This method creates a LocalDate instance from the year, month, and day values provided as arguments.
  3. ofYearDay(int year, int dayOfYear): This method creates a LocalDate instance from the year and day of the year values provided as arguments.
  4. getYear(): This method returns the year of the LocalDate instance.
  5. getMonth(): This method returns the month of the LocalDate instance.
  6. getDayOfMonth(): This method returns the day of the month of the LocalDate instance.
  7. getDayOfWeek(): This method returns the day of the week of the LocalDate instance.
  8. plusDays(long daysToAdd): This method returns a LocalDate instance with the specified number of days added to it.
  9. minusDays(long daysToSubtract): This method returns a LocalDate instance with the specified number of days subtracted from it.
  10. isAfter(LocalDate date): This method returns true if the LocalDate instance is after the specified date.
  11. isBefore(LocalDate date): This method returns true if the LocalDate instance is before the specified date.
  12. isEqual(LocalDate date): This method returns true if the LocalDate instance is equal to the specified date.
  13. format(DateTimeFormatter formatter): This method returns a formatted string representation of the LocalDate instance using the specified formatter.
  14. parse(CharSequence text, DateTimeFormatter formatter): This method parses the specified text string using the specified formatter and returns a LocalDate instance.

Java LocalDate Example:

Here is an example of using the Java LocalDate class to perform some common operations:

import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        // Get the current date
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);

        // Create a LocalDate instance for a specific date
        LocalDate specificDate = LocalDate.of(2022, 5, 15);
        System.out.println("Specific date: " + specificDate);

        // Add 10 days to the specific date
        LocalDate newDate = specificDate.plusDays(10);
        System.out.println("New date: " + newDate);

        // Check if the new date is after the current date
        boolean isAfterCurrentDate = newDate.isAfter(currentDate);
        System.out.println("Is new date after current date? " + isAfterCurrentDate);

        // Format the current date using a custom formatter
        String formattedDate = currentDate.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));
        System.out.println("Formatted date: " + formattedDate);

        // Parse a date string using a custom formatter
        LocalDate parsedDate = LocalDate.parse("07/04/2023", DateTimeFormatter.ofPattern("MM/dd/yyyy"));
        System.out.println("Parsed date: " + parsedDate);
    }
}

Output:

Current date: 2023-04-26
Specific date: 2022-05-15
New date: 2022-05-25
Is new date after current date? true
Formatted date: 04/26/2023
Parsed date: 2023-07-04

In this example, we first obtain the current date using the now() method of the LocalDate class. Then, we create another LocalDate instance for a specific date using the of() method, and add 10 days to it using the plusDays() method. We also check if the new date is after the current date using the isAfter() method.

Next, we format the current date using a custom formatter and display it on the console. Finally, we parse a date string using a custom formatter and obtain a LocalDate instance using the parse() method.