Get current date and time in Java
This post will discuss how to get the current date and time in Java.
1. Using LocalDateTime class
From Java 8 onwards, we can use the java.time package that offers a lot of improvement over the existing Java date and time APIs. To get the current date-time using the system clock and default time-zone, we can use the LocalDateTime.now() method. The java.time.LocalDateTime class represents a date and time without a time zone. It also provides various methods access the components (year, month, day, hour, minute, second, etc.) individually such as getYear(), getMonth(), getDayOfWeek(), getHour(), getMinute(), getSecond(), etc. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.time.LocalDateTime; public class Main { public static void main(String[] args) { // get the current date and time LocalDateTime datetime = LocalDateTime.now(); // print in ISO-8601 format System.out.println(datetime); // 2017-12-24T16:34:27.598873800 } } |
To get the current date and time as a formatted string, we can use the DateTimeFormatter class from the java.time.format package. It provides predefined and custom patterns for parsing and formatting date and time. We can pass the formatter to the format() method of the LocalDateTime class. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String dateTime = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME); // 2017-12-24T04:34:27.0871036 System.out.println(dateTime); } } |
We can also create a custom date-time formatter over the predefined date-time formatters using the DateTimeFormatter.ofPattern() method. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { String dateTime = DateTimeFormatter.ofPattern("MMM dd yyyy, hh:mm:ss a") .format(LocalDateTime.now()); System.out.println(dateTime); // Dec 24 2017, 04:34:27 pm } } |
2. Using ZonedDateTime class
To get the time-zone information from the system clock, consider switching to the java.time.ZonedDateTime class. It provides similar methods as the java.time.LocalDateTime class in addition to the time zone information. To obtain the current date-time from the system clock in the specified time-zone, we can specify a Zone ID to the ZonedDateTime.now() method. To format the date-time, we can specify a formatter using the format() method. Here’s an example:
|
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 |
import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class Main { public static void main(String[] args) { // get the current date and time with the default time zone ZonedDateTime now = ZonedDateTime.now(); System.out.println(now); // 2017-12-24T15:24:27.787019Z[Etc/UTC] // get the current date and time with a specific time zone ZonedDateTime nowInParis = ZonedDateTime.now(ZoneId.of("Europe/Paris")); System.out.println(nowInParis); // 2017-12-24T17:24:27.810635+02:00[Europe/Paris] // use predefined date-time formatters String nowInChicago = ZonedDateTime.now(ZoneId.of("America/Chicago")) .format(DateTimeFormatter.ISO_DATE_TIME); // 2017-12-24T05:42:28.0871036-06:00[America/Chicago] System.out.println(nowInChicago); // use custom date-time formatters String nowInKolkata = ZonedDateTime.now(ZoneId.of("Asia/Kolkata")) .format(DateTimeFormatter.ofPattern("MM.dd.yyy, hh.mm.ss a")); System.out.println(nowInKolkata); // 12.24.2017, 05.34:27 pm } } |
3. Using Date class
Before Java 8, the standard solution to get current date and time in Java is using the java.util.Date object. To initialize a Date object with the current date and time with millisecond precision, we can call its constructor without any arguments. The Date class is part of the legacy Java Date and Time API. Here’s an example:
|
1 2 3 4 5 6 7 8 |
import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); System.out.println(date); // Sun Dec 24 16:34:27 PDT 2017 } } |
To get the current date and time in the specified format, we can use the java.text.SimpleDateFormat class. It allows us to format and parse dates with predefined and user-defined patterns for date-time formatting. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { // create a custom formatter DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm:ss z"); // format the current date and time using the custom formatter String date = dateFormat.format(new Date()); System.out.println(date); // Sun, 24 Dec 2017, 16:34:27 IST } } |
4. Joda Time
The standard date and time classes before Java 8 are poorly designed and contain several flaws. The Joda-Time date-time library provides a high-quality replacement prior to Java 8. To get the full date and time with time-zone, we can use the DateTime class. The Joda-Time class also supports the time zone functionality through the DateTimeZone class. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
import org.joda.time.DateTime; public class Main { public static void main(String[] args) { DateTime now = DateTime.now(); System.out.println(now); // 2017-12-24T16:34:27.459-08:00 DateTime nowInSydney = DateTime.now(DateTimeZone.forID("Australia/Sydney")); System.out.println(nowInSydney); // 2017-12-24T23:47:11.949+11:00 } } |
5. Using Instant class
The Instant class models an instantaneous point on the timeline, independent of any time zone or calendar. To obtain the current instant using the system clock, we can use the Instant.now() method. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 |
import java.time.Instant; public class Main { public static void main(String[] args) { // Get the current instant from the system clock Instant instant = Instant.now(); System.out.println(instant); // 2017-12-24T12:05:28.940474900Z } } |
To obtain a Date instance from an Instant object, we can call the Date.from() method. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 |
import java.time.Instant; import java.util.Date; public class Main { public static void main(String[] args) { Date date = Date.from(Instant.now()); System.out.println(date); // Fri Dec 24 16:34:27 PDT 2017 } } |
That’s all about getting the current date and time in Java.
Related Posts:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)