Java – Get Current Date
To get current date in Java
- Import LocalDate class from java.time package.
- Create LocalDate object by calling static method now() of LocalDate class. LocalDate.now() returns the current date from the system clock in the default time zone.
- Call toString() method on this LocalDate object. toString() method returns the current date as a String.
Example
In the following program, we shall use LocalDate class of java.time package and get the the current date.
Java Program
</>
Copy
import java.time.LocalDate;
public class Example {
public static void main(String[] args) {
LocalDate dateObj = LocalDate.now();
String currentDate = dateObj.toString();
System.out.println(currentDate);
}
}
Output
2021-02-18
LocalDate.toString() method returns date in the format: yyyy-MM-dd
. We can format the date into any required pattern using DateTimeFormatter. Following are some of the tutorials, that provide examples to format Date.
Conclusion
In this Java Tutorial, we learned how to get current date using LocalDate class of java.time package, with example program.