Get Current Date and Time in Java
There are many ways to get current the date and time in Java. There are many classes that can be used to get current date and time in Java.
- java.time.format.DateTimeFormatter class
- java.text.SimpleDateFormat class
- java.time.LocalDate class
- java.time.LocalTime class
- java.time.LocalDateTime class
- java.time.Clock class
- java.util.Date class
- java.sql.Date class
- java.util.Calendar class
Get Current Date and Time: java.time.format.DateTimeFormatter
The LocalDateTime.now() method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints the current date and time. To format the current date, you can use DateTimeFormatter class which is included in JDK 1.8.
FileName: CurrentDateTimeExample1.java
Output:
2017/11/06 12:11:58
Get Current Date and Time: java.text.SimpleDateFormat
The SimpleDateFormat class is also used for formatting date and time. But it is old approach.
FileName: CurrentDateTimeExample2.java
Output:
06/11/2017 12:26:18
Get Current Date: java.time.LocalDate
The LocalDate.now() method returns the instance of the LocalDate class. If we print the instance of the LocalDate class, it prints the current date.
FileName: CurrentDateTimeExample3.java
Output:
2021-12-17
Get Current Time: java.time.LocalTime
The LocalTime.now() method returns the instance of LocalTime class. If we print the instance of LocalTime class, it prints the current time.
FileName: CurrentDateTimeExample4.java
Output:
15:55:10.424178667
Get Current Date & Time: java.time.LocalDateTime
The LocalDateTime.now() method returns the instance of LocalDateTime class. If we print the instance of LocalDateTime class, it prints the current date and time.
FileName: CurrentDateTimeExample5.java
Output:
2021-12-17T15:59:19.516010365
Get Current Date & Time: java.time.Clock
The Clock.systemUTC().instant() method returns the current date and time both.
FileName: CurrentDateTimeExample6.java
Output:
2021-12-17T16:04:03.930224479Z
Get Current Date & Time: java.util.Date
By printing the instance of java.util.Date class, you can print the current date and time in Java. There are two ways to do so.
1st way:
FileName: CurrentDateTimeExample7.java
Output:
Fri Dec 17 16:07:15 GMT 2021
2nd way:
FileName: CurrentDateTimeExample8.java
Output:
Fri Dec 17 16:07:15 GMT 2021
Get Current Date: java.sql.Date
By printing the instance of java.sql.Date class, you can print current date in Java. It doesn’t print time. This date instance is generally used to save the current date in the database.
FileName: CurrentDateTimeExample9.java
Output:
2021-12-17
Get Current Date & Time: java.util.Calendar
The Calendar class can be used to get the instance of the Date class. The getTime() method of the Calendar class returns the instance of java.util.Date. The Calendar.getInstance() method returns the instance of the Calendar class.
FileName: CurrentDateTimeExample10.java
Output:
Fri Dec 17 19:23:10 GMT 2021
Note: It is recommended to use the Calendar class for getting the current date and time in classical Date API. Since Java 8, you can use LocalDate, LocalTime, or LocalDateTime classes.