In this tutorial, we will show you how to get the current date time from the new Java 8 java.time.*
like Localdate, LocalTime, LocalDateTime, ZonedDateTime, Instant and also the legacy date time APIs like Date and Calendar.
Summary
- For new Java 8
java.time.*
APIs , we can use.now()
to get the current date-time and format it with DateTimeFormatter. - For legacy date-time APIs, we can use
new Date()
andCalendar.getInstance()
to get the current date-time and format it with SimpleDateFormat.
1. Get current date time in Java
The below are some code snippets to display the current date-time in Java.
For java.time.LocalDate
, uses LocalDate.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); // 2021/03/22
For java.time.localTime
, uses LocalTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(dtf.format(localTime)); // 16:37:15
For java.time.LocalDateTime
, uses LocalDateTime.now()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
For java.time.ZonedDateTime
, uses ZonedDateTime.now()
// get current date-time, with system default time zone
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
ZonedDateTime now = ZonedDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
System.out.println(now.getOffset()); // +08:00
// get current date-time, with a specified time zone
ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
System.out.println(dtf.format(japanDateTime)); // 2021/03/22 17:37:15
System.out.println(japanDateTime.getOffset()); // +09:00
For java.time.Instant
, uses Instant.now()
Instant now = Instant.now();
// convert Instant to ZonedDateTime
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(zonedDateTime));
For java.util.Date
, uses new Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15
For java.util.Calendar
, uses Calendar.getInstance()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15
2. java.time.LocalDate
For the java.time.LocalDate
, uses LocalDate.now()
to get the current date without a time-zone, and format it with the DateTimeFormatter
.
LocalDateExample.java
package com.favtuts.time; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { getCurrentDateWithJavaTimeLocalDate(); } static void getCurrentDateWithJavaTimeLocalDate() { // For java.time.LocalDate, uses LocalDate.now() DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd"); LocalDate localDate = LocalDate.now(); System.out.println(dtf.format(localDate)); // 2022/06/04 } }
Output
2022/06/04
3. java.time.LocalTime
For the java.time.LocalTime
, uses LocalDate.now()
to get the current time without a time-zone, and format it with the DateTimeFormatter
.
LocalTimeExample.java
package com.favtuts.time; import java.time.LocalTime; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { getCurrentTimeWithJavaTimeLocalTime(); } static void getCurrentTimeWithJavaTimeLocalTime() { // For java.time.localTime, uses LocalTime.now() DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss"); LocalTime localTime = LocalTime.now(); System.out.println(dtf.format(localTime)); // 16:37:15 } }
Output
16:37:15
4. java.time.LocalDateTime
For java.time.LocalDateTime
, uses LocalDateTime.now()
to get the current date time without a time-zone, and format it with the DateTimeFormatter
.
LocalDateTimeExample.java
package com.favtuts.time; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { getCurrentDateTimeWithJavaTimeLocalDateTime(); } static void getCurrentDateTimeWithJavaTimeLocalDateTime() { // For java.time.LocalDateTime, uses LocalDateTime.now() DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); LocalDateTime now = LocalDateTime.now(); System.out.println(dtf.format(now)); // 2022/06/04 10:04:09 } }
Output
2022/06/04 10:04:09
5. java.time.ZonedDateTime
For java.time.ZonedDateTime
, uses ZonedDateTime.now()
to get the current date time with the system default time zone, or a specified time zone.
ZonedDateTimeExample.java
package com.favtuts.time; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class CurrentDateTime { public static void main(String[] args) { getCurrentDateTimeWithJavaTimeZonedDateTime(); } static void getCurrentDateTimeWithJavaTimeZonedDateTime() { // For java.time.ZonedDateTime, uses ZonedDateTime.now() // Get default time zone System.out.println(ZoneOffset.systemDefault()); // Asia/Asia/Ho_Chi_Minh System.out.println(OffsetDateTime.now().getOffset()); // +07:00 // get current date-time, with system default time zone DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); ZonedDateTime now = ZonedDateTime.now(); System.out.println(dtf.format(now)); // 2022/06/04 10:08:54 System.out.println(now.getOffset()); // +07:00 // get current date-time, with a specified time zone ZonedDateTime japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Tokyo")); System.out.println(dtf.format(japanDateTime)); // 2022/06/04 12:08:54 System.out.println(japanDateTime.getOffset()); // +09:00 japanDateTime = now.withZoneSameInstant(ZoneId.of("Asia/Singapore")); System.out.println(dtf.format(japanDateTime)); // 2022/06/04 11:08:54 System.out.println(japanDateTime.getOffset()); // +08:00 } }
Output
Asia/Ho_Chi_Minh
+07:00
2022/06/04 10:10:49
+07:00
2022/06/04 12:10:49
+09:00
2022/06/04 11:10:49
+08:00
6. java.time.Instant
For java.time.Instant
, uses Instant.now()
to get the seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC), and later convert to other java.time.*
date time classes like LocalDate
, LocalDateTime
and ZonedDateTime
.
InstantExample.java
package com.favtuts.time; import java.time.*; import java.time.format.DateTimeFormatter; public class CurrentDateTime { private static final DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("uuuu/MM/dd"); private static final DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("HH:mm:ss"); private static final DateTimeFormatter dtfDateTime = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"); public static void main(String[] args) { getCurrentDateTimeWithJavaTimeInstant(); } static void getCurrentDateTimeWithJavaTimeInstant() { // seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC) Instant now = Instant.now(); System.out.println(now.toEpochMilli()); System.out.println(now.getEpochSecond()); System.out.println(now); // convert Instant to LocalDate LocalDate localDate = LocalDate.ofInstant(now, ZoneId.systemDefault()); System.out.println(dtfDate.format(localDate)); // convert Instant to localTime LocalTime localTime = LocalTime.ofInstant(now, ZoneId.systemDefault()); System.out.println(dtfTime.format(localTime)); // convert Instant to LocalDateTime LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault()); System.out.println(dtfDateTime.format(localDateTime)); // convert Instant to ZonedDateTime ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault()); System.out.println(dtfDateTime.format(zonedDateTime)); } }
Output
1654313120205
1654313120
2022-06-04T03:25:20.205688Z
2022/06/04
10:25:20
2022/06/04 10:25:20
7. java.util.Date (Legacy)
For the legacy java.util.Date
, uses new Date()
or new Date(System.currentTimeMillis()
to get the current date time, and format it with the SimpleDateFormat
.
DateExample.java
package com.favtuts.time; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class CurrentDateTime { public static void main(String[] args) { getCurrentDateWithLegacyJavaUtilDate(); } static void getCurrentDateWithLegacyJavaUtilDate() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15 // new Date() actually calls this new Date(long date) Date date2 = new Date(System.currentTimeMillis()); System.out.println(dateFormat.format(date)); // 2021/03/22 16:37:15 } }
Output
2022/06/04 10:29:45
2022/06/04 10:29:45
8. java.util.Calendar (Legacy)
For the legacy java.util.Calendar
, uses Calendar.getInstance()
to get the current date time, and format it with the SimpleDateFormat
.
CalendarExample.java
package com.favtuts.time; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class CurrentDateTime { public static void main(String[] args) { getCurrentDateTimeWithJavaUtilCalendar(); } static void getCurrentDateTimeWithJavaUtilCalendar() { DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime())); // 2021/03/22 16:37:15 } }
Output
2022/06/04 10:34:35
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/time