Get current date and time in java
This article will explain 7 different ways to get current date and time in java including the classes introduced in java 8.
Below is the list of methods covered.
java.time.LocalDateTime
java.time.LocalTime
java.time.ZonedDateTime
java.time.Instant
java.util.Date
java.util.Calendar
Java 8 introduced LocalDate class in
java.time
package, which simplifies getting current date.It has a static
now()
method which returns the current date. Example, LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); // 2022-07-02
now()
returns the date in yyyy-MM-dd format by default.
This can be changed using DateTimeFormatter
as shown below
LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter. ofPattern("dd-MM-yyyy"); System.out.println(formatter.format(currentDate)); // 02-07-2022
Method 2: Using LocalDateTime
This is another class added in java 8, which represents date and time instances without a time zone.
LocalDateTime
also has a now()
method, which returns the current date and time.
LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println(currentDateTime); // 2022-07-02T12:59:24.637521100
now()
returns the current date and time in yyyy-MM-ddThh:mm:ss.S format.
Here, ‘T’ is a separator between date and time.
Default format can be customized with DateTimeFormatter
as shown below
LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter. ofPattern("dd/MM/yyyy HH:mm:ss"); System.out.println(formatter.format(currentDateTime)); // 02/07/2022 12:59:24
Method 3: Using LocalTime
LocalTime in java 8 represents a time without time zone.
Its now() method returns the current time on the system on which it is executed.
LocalTime currentTime = LocalTime.now(); System.out.println(currentTime); // 13:05:56.029012100
Default format of time returned by now()
is HH:mm:ss.S, which can be customized using DateTimeFormatter
.
LocalTime currentTime = LocalTime.now(); DateTimeFormatter formatter = DateTimeFormatter. ofPattern("HH:mm:ss"); System.out.println(formatter.format(currentTime)); // 13:05:56
ZonedDateTime represents a date and time along with time zone information.
Its now() method returns the current date and time in the time zone of the machine on which it is executed. Example,
ZonedDateTime currentTime = ZonedDateTime.now(); System.out.println(currentTime); // 2022-07-02T14:05:21.282523100+05:30[Asia/Calcutta]
You can see now() returns the complete date, time and timezone along with its offset or difference from UTC time.
Default format is yyyy-MM-ddTHH:mm:ss.S<offset>[Timezone]
This can be customized with DateTimeFormatter
as shown below
ZonedDateTime currentTime = ZonedDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter. ofPattern("dd-MM-yyyy HH:mm:ss"); System.out.println(formatter.format(currentTime)); // 02-07-2022 14:08:12
It is also possible to get timezone and offset information from ZonedDateTime
as
ZonedDateTime currentTime = ZonedDateTime.now(); System.out.println("Timezone: " + currentTime.getZone()); System.out.println("Offset: " + currentTime.getOffset());
This prints
Timezone:Asia/Calcutta
Offset: +05:30
ZonedDateTime
can also be used to convert date time from one timezone to another.
Below code can be used to convert date time to est/edt timezone.
ZonedDateTime currentTime = ZonedDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter. ofPattern("dd-MM-yyyy hh:mm:ss"); System.out.println(formatter.format(currentTime)); ZonedDateTime estTime = currentTime.withZoneSameInstant( ZoneId.of("America/New_York")); System.out.println(formatter.format(estTime));
Output is
02-07-2022 14:26:01
02-07-2022 04:56:01
Method 5: Using Instant
Java 8 Instant
class represents a moment or the current instant in UTC, which means that it is the current date and time.
To get the current date and time in your respective timezone, it should be converted as shown below
Instant utcTime = Instant.now(); System.out.println(utcTime); ZonedDateTime currentTime = utcTime.atZone(ZoneId.systemDefault()); System.out.println(currentTime);
This outputs
2022-07-02T09:07:31.345280100Z
2022-07-02T14:37:31.345280100+05:30[Asia/Calcutta]
As before, the format can be customized with DateTimeFormatter
.
java.util.Date
is a traditional class present since java 1.0 version.Constructor of date creates its object holding current date, time and timezone information.
Date date = new Date(); System.out.println(date);
Output is
Sat Jul 02 15:17:10 IST 2022
You can see that the value returned by date is very long. It can be customized using SimpleDateFormat
as shown below
Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(format.format(date)); // 02/07/2022 15:17:10
There is another way to create a date object using its overloaded constructor and passing it current system time as long
value as shown below
Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); System.out.println(format.format(date));
Remember that the constructor new Date()
internally invokes the constructor which takes a long
value.
This is called constructor chaining.
Method 7: Using Calendar
java.util.Calendar
is also a legacy class present since java 1.0 version.
To create an instance of Calendar
, use its static getInstance()
method.
Its getTime()
method returns a Date
, containing the current date and time information as shown below
Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime());
getTime()
internally calls the Date
constructor passing it the current system time as a long value.
So, there were the 7 methods to get current date and time in java.
If you are working on java version 8 and above, then it is recommended to use java.time
classes such as LocalDate
, LocalDateTime
, LocalTime
, ZonedDateTime
, Instant
as discussed in earlier sections.