How do I get the current date and time in Java?

Abdul D.
jump to solution

The Problem

I don’t know how to get the current date and time in Java.

The Solution

You can the current date and time using classes from the java.time package introduced in Java 8.

We will demonstrate how to use the following recommended java.time classes:

  • LocalDateTime
  • LocalDate
  • LocalTime
  • ZonedDateTime

Note: You need to be running Java version 8 or later to use java.time classes.

Using LocalDateTime

The LocalDateTime class is commonly used to get the current date and time without time zone information:

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date and Time: " + currentDateTime);
    }
}

This prints:

Current Date and Time: 2024-10-15T14:30:00.123

Using LocalDate and LocalTime

If you need only the current date or the current time, you can use LocalDate or LocalTime:

import java.time.LocalDate;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalDate currentDate = LocalDate.now();
        LocalTime currentTime = LocalTime.now();
        System.out.println("Current Date: " + currentDate);
        System.out.println("Current Time: " + currentTime);
    }
}

This prints:

Current Date: 2024-10-15
Current Time: 14:30:00.123

Using ZonedDateTime

If you need the current date and time with time zone information, you can use ZonedDateTime:

import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime currentDateTimeWithZone = ZonedDateTime.now();
        System.out.println("Current Date and Time with Time Zone: " + currentDateTimeWithZone);
    }
}

This prints:

Current Date and Time with Time Zone: 2024-10-15T14:30:00.123-04:00[America/New_York]

Using ZonedDateTime and ZoneId

To get the current date and time for a specific time zone, use ZonedDateTime with ZoneId:

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        ZoneId zoneId = ZoneId.of("Africa/Johannesburg");
        ZonedDateTime currentDateTimeInZone = ZonedDateTime.now(zoneId);
        System.out.println("Current Date and Time in Johannesburg: " + currentDateTimeInZone);
    }
}

In this example, ZoneId.of("Africa/Johannesburg") specifies the time zone for SAST, and ZonedDateTime.now(zoneId) provides the current date and time in the specified time zone.

This prints:

Current Date and Time with Time Zone: 2024-10-15T21:30:00.123+02:00[Africa/Johannesburg]

Summary

You can get local time using the following thread-safe classes from the java-time package:

  • LocalDateTime.now() gets the current date and time without time zone information.
  • LocalDate.now() gets only the current date.
  • LocalTime.now() gets only the current time.
  • ZonedDateTime.now() gets the current date and time with time zone information.
  • ZonedDateTime.now(ZoneId.of("zone")) gets the current date and time for a specific time zone.

Considered "not bad" by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world's best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Sentry