Java Program to Get the Current Date and Time
Since Java 8, the java.time package is the standard way to work with dates and times. Its classes are easy to read, cannot be changed after creation (immutable), and handle time zones correctly.
The outputs on this page show the date and time when the programs were run, so yours will be different.
1. Current Date and Time Using java.time
Each class has a static now() method:
LocalDate: date only (year, month, day).LocalTime: time only (hours, minutes, seconds, nanoseconds).LocalDateTime: date and time, without a time zone.ZonedDateTime: date and time in a specific time zone.Instant: a moment on the UTC timeline, useful for timestamps.
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class CurrentDateTime {
public static void main(String[] args) {
System.out.println("LocalDate: " + LocalDate.now());
System.out.println("LocalTime: " + LocalTime.now());
System.out.println("LocalDateTime: " + LocalDateTime.now());
System.out.println("ZonedDateTime: " + ZonedDateTime.now(ZoneId.of("Asia/Kolkata")));
System.out.println("Instant (UTC): " + Instant.now());
}
}
LocalDate: 2026-10-17 LocalTime: 19:13:59.515003 LocalDateTime: 2026-10-17T19:13:59.515069 ZonedDateTime: 2026-10-17T19:13:59.515306+05:30[Asia/Kolkata] Instant (UTC): 2026-10-17T13:43:59.515382Z
2. Format the Date and Time with DateTimeFormatter
The default output above is in ISO format. DateTimeFormatter.ofPattern() lets you choose your own layout. Common pattern letters are dd (day), MM (month number), MMMM (month name), yyyy (year), HH (hour 0–23), hh (hour 1–12), mm (minutes), ss (seconds), a (AM/PM) and EEEE (day name).
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class FormatDateTime {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
DateTimeFormatter f2 = DateTimeFormatter.ofPattern("EEEE, d MMMM yyyy", Locale.ENGLISH);
DateTimeFormatter f3 = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
System.out.println(now.format(f1));
System.out.println(now.format(f2));
System.out.println(now.format(f3));
}
}
17/10/2026 19:13:59 Saturday, 17 October 2026 07:13 PM
3. Get the Year, Month, Day and Time Separately
Getter methods return individual parts of the date and time.
import java.time.LocalDateTime;
public class DateTimeParts {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println("Year: " + now.getYear());
System.out.println("Month: " + now.getMonth() + " (" + now.getMonthValue() + ")");
System.out.println("Day: " + now.getDayOfMonth() + ", " + now.getDayOfWeek());
System.out.println("Hour: " + now.getHour());
System.out.println("Minute: " + now.getMinute());
}
}
Year: 2026 Month: OCTOBER (10) Day: 17, SATURDAY Hour: 19 Minute: 14
4. Current Date Using the Older Date Class
Older code often uses java.util.Date with SimpleDateFormat. They still work, but the java.time classes above are recommended for new code.
import java.text.SimpleDateFormat;
import java.util.Date;
public class LegacyDate {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println("Formatted: " + format.format(now));
System.out.println("Milliseconds since 1970: " + now.getTime());
}
}
Formatted: 17-10-2026 19:14:00 Milliseconds since 1970: 1792244640304
5. Summary
LocalDate.now(),LocalTime.now()andLocalDateTime.now()return the current date and time.- Use
ZonedDateTimefor a specific time zone andInstantfor UTC timestamps. DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")formats dates in any layout.- Prefer
java.timeover the olderDateandSimpleDateFormatclasses.