A new date time api added in Java 8 in java.time
package. In this tutorial we have covered examples to get current Date Time in java 8.
1. Get Current Date without time
java.time.LocalDate is used to get the get current Date. Following example demonstrates how to get current date using LocalDate.now()
method.
public class Java8CurrentDateDemo { public static void main(String[] args) throws ParseException { // Obtains the current date in the default time-zone. LocalDate date1 = LocalDate.now(); // System clock converting to date using the UTC time-zone. LocalDate date2 = LocalDate.now(Clock.systemUTC()); // Obtains the current date in the specified time-zone. LocalDate date3 = LocalDate.now(ZoneId.of("GMT+05:30")); System.out.println("current system date : " + date1); System.out.println("Zone UTC current date : " + date2); System.out.println("Zone GMT current date : " + date3); } }
Output :
current system date : 2020-03-13 Zone UTC current date : 2020-03-13 Zone GMT current date : 2020-03-13
2. Get Current Date with Time
java.time.LocalDateTime is used to get the get current Date with time. Following example demonstrates how to get current date time using LocalDateTime.now()
method. We can convert LocaldateTime
to LocalDate
and LocalTime
.
public class Java8CurrentDateTimeDemo { public static void main(String[] args) throws ParseException { LocalDateTime date1 = LocalDateTime.now(); LocalDateTime date2 = LocalDateTime.now(Clock.systemUTC()); LocalDateTime date3 = LocalDateTime.now(ZoneId.of("GMT+05:30")); // Converting LocalDateTime to LocalDate System.out.println("LocalDate : " + date1.toLocalDate()); // Converting LocalDateTime to LocalTime System.out.println("LocalTime : " + date1.toLocalTime()); System.out.println("Current system date time : " + date1); System.out.println("Current UTC date time : " + date2); System.out.println("Current GMT date time : " + date3); } }
Output :
LocalDate : 2020-03-13 LocalTime : 23:24:23.550696500 Current system date time : 2020-03-13T23:24:23.550696500 Current UTC date time : 2020-03-13T15:24:23.550696500 Current GMT date time : 2020-03-13T20:54:23.550696500
3. using ZonedDateTime
ZonedDateTime
is an immutable representation of a date-time with a time-zone. Following example shows you how to get current date time using ZonedDateTime
.
public class Java8CurrentZonedDateTimeDemo { public static void main(String[] args) throws ParseException { // current date-time from the system clock in the default time-zone. ZonedDateTime date1 = ZonedDateTime.now(); // System clock converting to date and time using the UTC time-zone. ZonedDateTime date2 = ZonedDateTime.now(Clock.systemUTC()); // Obtains the current date-time from the system clock in the specified time-zone. ZonedDateTime date3 = ZonedDateTime.now(ZoneId.of("GMT+05:30")); System.out.println("Current system date time : " + date1); System.out.println("Current UTC date time : " + date2); System.out.println("Current GMT date time : " + date3); } }
Output :
Current system date time : 2020-03-13T23:27:11.280883200+08:00[Asia/Singapore] Current UTC date time : 2020-03-13T15:27:11.281908Z Current GMT date time : 2020-03-13T20:57:11.281908+05:30[GMT+05:30]
4. Conclusion
In Java There are several ways to get Current date time. In this tutorial we have covered How to get current Date Time in java 8 using LocalDate, LocalDateTime and ZonedDateTime classes.