CodeGym/Java Blog/Core Java/Java LocalDate class
Author
Artem Divertitto
Senior Android Developer at United Tech

Java LocalDate class

Published in the Core Java group
members
Java LocalDate class is immutable, which means once an instance is created, you cannot modify it. LocalDate provides many useful methods to manipulate and query dates, such as adding or subtracting days, months, or years, getting the day of the week, or checking if a date is before or after another date.

LocalDate Example

Let's start with a simple example that creates a LocalDate instance for the current date and prints it to the console:
import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Today's date: " + today);
    }
}
In this example, we import the LocalDate class from the java.time package and create a new instance of LocalDate called today using the static now() method. This method returns a LocalDate object representing the current date based on the system clock in the default time zone. We then print the current date to the console using the println() method of the System.out object. The output will look something like this:

Output

Today's date: 2023-05-01

LocalDate Constructors

Besides the now() method, LocalDate provides several other constructors that allow you to create LocalDate instances from different sources. For example, you can create a LocalDate from a specific year, month, and day:
import java.time.LocalDate;

public class LocalDateConstructorExample {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2023, 5, 1);

        System.out.println("Date 1: " + date1);
    }
}
This code creates a new LocalDate instance called date1 with the year 2023, month May (represented by the value 5), and day 1. The output will be:

Output

Date 1: 2023-05-01

LocalDate Manipulation

One of the most useful features of LocalDate is the ability to manipulate dates. For example, you can add or subtract days, months, or years using the plus and minus methods:
import java.time.LocalDate;

public class LocalDateManipulationExample {
    public static void main(String[] args) {

        LocalDate date3 = LocalDate.parse("2023-05-01");
        LocalDate date4 = date3.plusDays(1);
        LocalDate date5 = date3.minusMonths(1);
        LocalDate date6 = date3.plusYears(1);

        System.out.println("Date 3: " + date3);
        System.out.println("Date 4: " + date4);
        System.out.println("Date 5: " + date5);
        System.out.println("Date 6: " + date6);
   }
}
In this example, we first create a new LocalDate instance called date3 by parsing the string "2023-05-01". We then create three new LocalDate instances called date4, date5, and date6 by adding or subtracting days, months, or years from date3. The output will be:
Date 3: 2023-05-01 Date 4: 2023-05-02 Date 5: 2023-04-01 Date 6: 2024-05-01

Comparing LocalDates

LocalDate also provides methods to compare dates. You can check if two LocalDate instances are equal, or if one is before or after another using the equals(), isBefore(), and isAfter() methods:
import java.time.LocalDate;

public class LocalDateCompareExample {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.parse("2023-05-01");
        LocalDate date2 = LocalDate.parse("2023-05-02");

        System.out.println("Are the dates equal? " + date1.equals(date2));
        System.out.println("Is date1 before date2? " + date1.isBefore(date2));
        System.out.println("Is date2 after date1? " + date2.isAfter(date1));
    }
}
In this example, we create two LocalDate instances called date1 and date2, and then use the equals(), isBefore(), and isAfter() methods to compare them. The output will be in boolean form i.e. true or false:
Are the dates equal? false Is date1 before date2? true Is date2 after date1? true

LocalDateTime

If you need to represent a date and time, you can use the LocalDateTime class, which is similar to LocalDate but includes a time component. LocalDateTime provides methods to manipulate and query both the date and time parts. Here's an example of creating a LocalDateTime instance:
import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime datetime = LocalDateTime.now();
        System.out.println("Current date and time: " + datetime);
    }
}
This code imports the LocalDateTime class and creates a new instance called datetime using the now() method. The output will look something like this:
Current date and time: 2023-05-01T15:30:00.123456789
Note: ‘T’ in output stands to indicate starting of the timestamp. Java LocalDate class - 1

Conclusion

The Java LocalDate class is useful for working with dates in Java. It provides many methods to manipulate and query dates, making it easy to add or subtract days, months, or years, check if a date is before or after another date, and more. If you need to work with dates and times together, you can use the LocalDateTime class instead. Feel free to experiment with this class and share your results with us! Get back to this post whenever you feel stuck. Happy Coding!
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet