To get the current local date, you can use the now() method of the LocalDate class in Java 8.
LocalDate class represents the local date which is based on the current running system and its date-time zone.
Let's see the method signature.
public static LocalDate now()
This method does not take any argument but returns an object of the LocalDate class that represents the current date.
See the below example.
/*
* Code example to get current local date in Java
*/
import java.time.LocalDate;
public class JExercise {
public static void main(String[] args) {
// Current date
LocalDate date = LocalDate.now();
// Displaying current date
System.out.println("Date : "+date);
}
}
Output:
Date: 2022-03-28
This is the overloaded method of the now() that requires a Clock class object if you wish to get a local date based on the clock.
public static LocalDate now​(Clock clock)
This method takes single parameter and returns the local date from the specified clock.
import java.time.Clock;
/*
* Code example to get the current local date in Java
*/
import java.time.LocalDate;
public class JExercise {
public static void main(String[] args) {
// Current date
LocalDate date = LocalDate.now(Clock.systemDefaultZone());
// Displaying current date
System.out.println("Date : "+date);
}
}
Output:
Date : 2022-03-28
This is another overloaded method of the now() method. It is used to get the local date based on the zone id.
public static LocalDate now​(ZoneId zone)
This method takes a single argument and returns the current date from the system clock in the specified time zone.
/*
* Code example to get the current local date in Java
*/
import java.time.LocalDate;
import java.time.ZoneId;
public class JExercise {
public static void main(String[] args) {
// Current date
LocalDate date = LocalDate.now(ZoneId.of("Antarctica/McMurdo"));
// Displaying current date
System.out.println("Date : "+date);
date = LocalDate.now(ZoneId.of("Asia/Kolkata"));
// Displaying current date
System.out.println("Date : "+date);
}
}
Output:
Date : 2022-03-29
Date : 2022-03-28
To find the zone id for your locale, refer to this table: time-zone id