# Java Current Date and Time with LocalDateTime

> Use java.time classes like LocalDateTime, LocalDate, LocalTime, and ZonedDateTime to get the current date and time in Java with time zone support

**URL:** https://sentry.io/answers/how-do-i-get-the-current-date-and-time-in-java/

---

## 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:

```java
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`:

```java
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`:

```java
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`:

```java
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.

---

*Source: [sentry.io/answers/how-do-i-get-the-current-date-and-time-in-java/](https://sentry.io/answers/how-do-i-get-the-current-date-and-time-in-java/)*
