[Date] 1 How to get current date time

The main API for dates, times, instants, and durations.

Posted by Mr.Vincent on 2021-07-13
Estimated Reading Time 7 Minutes
Words 1.2k In Total
Viewed Times

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() and Calendar.getInstance() to get the current date-time and format it with SimpleDateFormat.

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().

1
2
3
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().

1
2
3
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().

1
2
3
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().

1
2
3
4
5
6
7
8
9
10
// 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().

1
2
3
4
5
6
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().

1
2
3
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().

1
2
3
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

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.

Example: LocalDate

1
2
3
4
5
6
7
8
9
10
11
12
package com.mkyong.app;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class LocalDateExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd");
LocalDate localDate = LocalDate.now();
System.out.println(dtf.format(localDate)); // 2021/03/22
}
}

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.

Example: LocalTime

1
2
3
4
5
6
7
8
9
10
11
12
package com.mkyong.app;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class LocalTimeExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime = LocalTime.now();
System.out.println(dtf.format(localTime)); // 16:37:15
}
}

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.

Example: LocalDateTime

1
2
3
4
5
6
7
8
9
10
11
12
package com.mkyong.app;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeExample {
public static void main(String[] args) {
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
}
}

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.

Example: ZonedDateTime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.mkyong.app;

import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZonedDateTimeExample {
public static void main(String[] args) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss");

// Get default time zone
System.out.println(ZoneOffset.systemDefault()); // Asia/Kuala_Lumpur
System.out.println(OffsetDateTime.now().getOffset()); // +08:00

// get current date time, with +08:00
ZonedDateTime now = ZonedDateTime.now();
System.out.println(dtf.format(now)); // 2021/03/22 16:37:15
System.out.println(now.getOffset()); // +08:00

// get get current date time, with +09:00
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
}
}

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.

Example: Instant

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.mkyong.app;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class InstantExample {
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) {
// seconds passed since the Unix epoch time (midnight of January 1, 1970 UTC)
Instant now = Instant.now();

// convert Instant to LocalDate
LocalDate localDate = LocalDate.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDate.format(localDate)); // 2021/03/22

// convert Instant to localTime
LocalTime localTime = LocalTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfTime.format(localTime)); // 16:37:15

// convert Instant to LocalDateTime
LocalDateTime localDateTime = LocalDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(localDateTime)); // 2021/03/22 16:37:15

// convert Instant to ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.systemDefault());
System.out.println(dtfDateTime.format(zonedDateTime)); // 2021/03/22 16:37:15
}
}

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.

Example: Date

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.mkyong.app;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {
public static void main(String[] args) {
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
}
}

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.

Example: Calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.mkyong.app;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarExample {
public static void main(String[] args) {
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
}
}

References:https://mkyong.com/java/java-how-to-get-current-date-time-date-and-calender/


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !