In Java, there are multiple ways to get the current time and date. Numerous choices are available with a range of APIs to print the current time and date.
There are legacy APIs and Since Java 1.8 we have more options to print current time and date.
Legacy APIs
Legacy APIs to print current time and date in java.util package,
- Date
- Calendar(Since Java 1.1)
- SimpleDateFormat
Java 8 Date and Time APIs
APIs introduced in Java 1.8 to print current time and date. These APIs are available in java.time package,
- LocalDate
- LocalTime
- Instant
- LocalDateTime
- ZonedDateTime
- OffsetDateTime
- Clock
- DateTimeFormatter
With Date, Calendar, and System.currentTimeMillis() the current date and time can be printed using SimpleDateFormat.
LocalDate, LocalTime, LocalDateTime, ZonedDateTime and OffsetDateTime print date, time or timestamp in current zone format.
Instant and Clock Java APIs print dates in UTC format.
LocalDate.now() prints the date in YYYY-MM-DD format in the default time zone. Example 2023-05-01.
LocalTime.now() prints the time in Hour Minute Second Nanosecond format in the default time zone. Example 12:38:12.042676700.
now() is a static method available both in LocalDate and LocalTime classes.
ZonedDateTime.now() prints the timestamp with the current time zone name also. Example 2023-05-01T10:45:48.907921900+05:30[Asia/Calcutta]
RFC_1123_DATE_TIME format prints the date time in Mon, 1 May 2023 12:38:12 GMT format.
JavaCurrentDateAndTime.java
package com.digitizedpost.date;
import java.text.SimpleDateFormat;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class JavaCurrentDateAndTime {
private void getCurrentDateAndTime() {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
System.out.println("Formatted date: " + formatter.format(date));
String timeStamp = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println("Formatted date: " + timeStamp);
long currentTimeMillis = System.currentTimeMillis();
System.out.println("CurrentTime in Millis: " + currentTimeMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date date2 = new Date(currentTimeMillis);
System.out.println("Formatted date: " + dateFormat.format(date2));
System.out.println("Today's date: " + LocalDate.now());
System.out.println("Current time: " + LocalTime.now());
System.out.println("Current Date and Timestamp(UTC): " + Instant.now());
System.out.println("Current Date and Timestamp: " + LocalDateTime.now());
System.out.println("ZonedDateTime.now() : " + ZonedDateTime.now());
System.out.println("OffsetDateTime.now() : " + OffsetDateTime.now());
System.out.println("Current Date and Timestamp(UTC): " + Clock.system(ZoneId.of("Asia/Kolkata")).instant());
Clock clock = Clock.systemDefaultZone();
Instant instant = clock.instant();
System.out.println("Current Date and Timestamp(UTC): " + instant);
Instant clockInstant = Instant.now(clock);
System.out.println("Current Date and Timestamp(UTC): " + clockInstant);
System.out.println("Date Time with Day Month Time Format:"+ ZonedDateTime.now().format(DateTimeFormatter.RFC_1123_DATE_TIME));
}
public static void main(String[] args) {
new JavaCurrentDateAndTime().getCurrentDateAndTime();
}
}
Output:
The JavaCurrentDateAndTime program prints the current date, current time, or timestamp in specified formats.
Formatted date: 01/05/2023 10:45:48
Formatted date: 01-05-2023 10:45:48
CurrentTime in Millis: 1682918148843
Formatted date: May 01,2023 10:45
Today's date: 2023-05-01
Current time: 10:45:48.883985800
Current Date and Timestamp(UTC): 2023-05-01T05:15:48.884982300Z
Current Date and Timestamp: 2023-05-01T10:45:48.905928200
ZonedDateTime.now() : 2023-05-01T10:45:48.907921900+05:30[Asia/Calcutta]
OffsetDateTime.now() : 2023-05-01T10:45:48.909925100+05:30
Current Date and Timestamp(UTC): 2023-05-01T05:15:48.910914Z
Current Date and Timestamp(UTC): 2023-05-01T05:15:48.910914Z
Current Date and Timestamp(UTC): 2023-05-01T05:15:48.911911400Z
Date Time with Day Month Time Format: Mon, 1 May 2023 10:45:48 +0530