Java
Programming
Date and Time
Coding Tutorial
Information Technology

How to get the current date/time in Java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In modern Java, the best way to get the current date or time is usually with the java.time API. Which class you choose depends on what you actually mean by "current date/time": a local calendar date, a local date-time, a time with zone information, or a machine-readable instant.

Pick the Right Class First

The main options are:

  • 'LocalDate for just the date'
  • 'LocalTime for just the time'
  • 'LocalDateTime for date and time without a time zone'
  • 'ZonedDateTime for date and time with a zone'
  • 'Instant for an absolute point in time'

Choosing the right type is more important than memorizing the method name, because the method is usually just now().

Current Local Date and Time

If you want the current date and time in the system default zone, but you do not need to store the zone explicitly:

java
1import java.time.LocalDate;
2import java.time.LocalDateTime;
3import java.time.LocalTime;
4
5public class Main {
6    public static void main(String[] args) {
7        LocalDate today = LocalDate.now();
8        LocalTime currentTime = LocalTime.now();
9        LocalDateTime currentDateTime = LocalDateTime.now();
10
11        System.out.println(today);
12        System.out.println(currentTime);
13        System.out.println(currentDateTime);
14    }
15}

This is the most common everyday Java usage.

Current Date and Time with a Zone

If the time zone matters, use ZonedDateTime.

java
1import java.time.ZoneId;
2import java.time.ZonedDateTime;
3
4public class Main {
5    public static void main(String[] args) {
6        ZonedDateTime local = ZonedDateTime.now();
7        ZonedDateTime utc = ZonedDateTime.now(ZoneId.of("UTC"));
8        ZonedDateTime toronto = ZonedDateTime.now(ZoneId.of("America/Toronto"));
9
10        System.out.println(local);
11        System.out.println(utc);
12        System.out.println(toronto);
13    }
14}

This is better than LocalDateTime when the result must be interpreted consistently across systems.

Machine-Friendly Timestamps with Instant

If you want a precise point on the UTC timeline, use Instant.

java
1import java.time.Instant;
2
3public class Main {
4    public static void main(String[] args) {
5        Instant now = Instant.now();
6        System.out.println(now);
7    }
8}

Instant is ideal for:

  • event timestamps
  • database audit fields
  • log correlation
  • distributed systems

It is usually the best storage type when you do not want ambiguity about time zones.

Formatting the Current Date and Time

Raw toString() output is often fine for debugging, but user-facing output should usually be formatted.

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalDateTime now = LocalDateTime.now();
7        DateTimeFormatter formatter =
8                DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
9
10        System.out.println(now.format(formatter));
11    }
12}

Formatting should happen at the presentation boundary. Keep your internal representation as a proper date-time type.

Legacy APIs

Older Java code often uses:

  • 'java.util.Date'
  • 'java.util.Calendar'

Example:

java
1import java.util.Date;
2
3public class Main {
4    public static void main(String[] args) {
5        Date now = new Date();
6        System.out.println(now);
7    }
8}

These APIs still exist, but for new code java.time is the preferred design because it is clearer, more consistent, and much easier to work with correctly.

Choosing by Use Case

A practical rule of thumb is:

  • display today's date: LocalDate.now()
  • timestamp a local business event: LocalDateTime.now()
  • represent a real-world zoned time: ZonedDateTime.now(...)
  • persist or compare a global timestamp: Instant.now()

This avoids a lot of unnecessary confusion later.

Common Pitfalls

Using LocalDateTime when you really need zone-aware or UTC-safe timestamps creates ambiguity across systems.

Formatting too early and storing strings instead of proper date-time types makes later computation harder.

Mixing legacy Date and modern java.time classes without a reason leads to messy code and unnecessary conversions.

Assuming the system default time zone is always the correct business time zone can create subtle bugs in deployed systems.

Summary

  • In modern Java, prefer the java.time API for current date and time values.
  • Use LocalDate, LocalTime, or LocalDateTime for local calendar values.
  • Use ZonedDateTime when time-zone context matters.
  • Use Instant for absolute timestamps and storage-friendly event times.
  • Pick the type by use case first, then call now() on that type.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.