Convert Instant to LocalDateTime in Java [3 ways]

In this post, I will be sharing how to convert Instant to LocalDateTime in Java. There are three ways to achieve our goal of converting Instant to LocalDateTime in Java:

1. Using LocalDateTime class ofInstant() method [Recommended]

2. Using ZonedDateTime

3. Using OffsetDateTime

Convert Instant to LocalDateTime in Java

1. Using LocalDateTime class ofInstant() method

We can easily convert Instant to LocalDateTime in Java using LocalDateTime.ofInstant() method passing instant object and ZoneOffset as input- arguments as shown below in the example:

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;

public class InstantToLocalDateTime {
    public static void main(String args[]) {
        // Get Instant object
        Instant instant = Instant.now();
        System.out.println("Current instant at UTC/GMT is: "+ instant);
        
        // Convert Instant to LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
        System.out.println("LocalDateTime is: "+ localDateTime);
    }
}


Output:
Current instant at UTC/GMT is: 2024-01-28T03:56:50.493753433Z
LocalDateTime is: 2024-01-28T03:56:50.493753433


2. Using ZonedDateTime

1. We can convert Instant to ZonedDateTime using ZonedDateTime.ofInstant() method passing instant object and ZoneOffset as input arguments.

2. Convert ZonedDateTime to LocalDateTime using toLocalDateTime() method as shown below in the example:

import java.time.ZonedDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.LocalDateTime;

public class InstantToLocalDateTime2 {
    public static void main(String args[]) {
        // Get Instant object
        Instant instant = Instant.now();
        System.out.println("Current instant at UTC/GMT is: "+ instant);
        
        // Convert Instant to ZonedDateTime
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
        System.out.println("ZonedDateTime is: "+ zonedDateTime);
        
        // Convert ZonedDateTime to LocalDateTime
        LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
        System.out.println("LocalDateTime is: "+ localDateTime);
    }
}


Output:
Current instant at UTC/GMT is: 2024-01-28T04:58:13.491775578Z
ZonedDateTime is: 2024-01-28T04:58:13.491775578Z
LocalDateTime is: 2024-01-28T04:58:13.491775578


3. Using OffsetDateTime

1. We can convert Instant to OffsetDateTime using OffsetDateTime.ofInstant() method passing instant object and ZoneOffset as input arguments.

2. Convert OffsetDateTime to LocalDateTime using toLocalDateTime() method as shown below in the example:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.time.LocalDateTime;

public class InstantToLocalDateTime3 {
    public static void main(String args[]) {
        // Get Instant object
        Instant instant = Instant.now();
        System.out.println("Current instant at UTC/GMT is: "+ instant);
        
        // Convert Instant to OffsetDateTime
        OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
        System.out.println("OffsetDateTime is: "+ offsetDateTime);
        
        // Convert OffsetDateTime to LocalDateTime
        LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
        System.out.println("LocalDateTime is: "+ localDateTime);
    }
}


Output:
Current instant at UTC/GMT is: 2024-01-28T05:09:25.938670885Z
OffsetDateTime is: 2024-01-28T05:09:25.938670885Z
LocalDateTime is: 2024-01-28T05:09:25.938670885


That's all for today. Please mention in the comments if you know any other way of converting Instant to LocalDateTime in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry