To get the current date in Java, you can use the LocalDate
class from the java.time
package, which is part of the new Java Date and Time API introduced in Java 8.
Here's an example of how to get the current date in Java using the LocalDate
class:
import java.time.LocalDate; LocalDate currentDate = LocalDate.now(); System.out.println("The current date is: " + currentDate);
In the above example, the now()
method of the LocalDate
class is used to get the current date, which is then stored in the currentDate
variable. The date is then printed to the console using the println()
method.
The LocalDate
class represents a date (year, month, and day) without a time-zone in the ISO-8601 calendar system. If you want to include the time as well as the date, you can use the LocalDateTime
class from the java.time
package.
For example:
import java.time.LocalDateTime; LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println("The current date and time is: " + currentDateTime);
In this case, the LocalDateTime
class represents a date and time with a time-zone in the ISO-8601 calendar system.
For more information on working with dates and times in Java, you can refer to the documentation for the java.time
package in the Java API.