-->
Skip to main content

Convert Between LocalDateTime to Instant In Java [2021]

Convert Between LocalDateTime to Instant In Java [2021] 

In this tutorial, we will cover how to Convert Between LocalDateTime to Instant using 
  • localdatetime.toInstant(ZoneOffset.UTC),
  • localdatetime.atZone(ZoneId.systemDefault()).toInstant(),
  • localdatetime.toEpochSecond(ZoneOffset.UTC)
and Instant to LocalDateTime using
  • LocalDateTime.ofInstant(instant, ZoneId.systemDefault())
  • Timestamp.from(instant).toLocalDateTime()
LocalDateTime provides us date-time without timezone often viewed as year-month-day-hour-minute-second where as Instant provides us instantaneous point on the time-line. Instant class stores a long representing epoch-seconds and an int representing nanosecond-of-second.

Earlier we recent tutorials we covered how we can convert LocalDate to Instant and Instant to LocalDate in Java. Now we see Conversion between LocalDateTime to Instant using these methods with examples.

LocalDateTime to Instant

Convert LocalDateTime to Instant

In this, we are going to convert LocalDateTime to instant and show all the methods in action with examples.

Example:

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

 public class LocalDateTimeToInstant {
   public static void main(String[] args) {

    LocalDateTime localdatetime = LocalDateTime.now();
    System.out.println(localdatetime);

    // Using LocalDateTime.toInstant method
    Instant instant =
      localdatetime.toInstant(ZoneOffset.UTC);
    System.out.println(instant);

    // Using LocalDateTime.atZone().toInstant()
    Instant instant1 = localdatetime
     .atZone(ZoneId.systemDefault()).toInstant();
    System.out.println(instant1);

    // Using Instant.ofEpochSecond
    long timeInSeconds =
      localdatetime.toEpochSecond(ZoneOffset.UTC);
    Instant instant2 =
      Instant.ofEpochSecond(timeInSeconds);
    System.out.println(instant2);
   }
 }

Output:

2020-07-22T18:44:57.277
2020-07-22T18:44:57.277Z
2020-07-22T13:14:57.277Z
2020-07-22T18:44:57Z

Instant to LocalDateTime

Convert Instant to LocalDateTime

In this example, we are going to convert Instant to LocalDateTime using examples.

Example:

 import java.sql.Timestamp;
 import java.time.Instant;
 import java.time.LocalDateTime;
 import java.time.ZoneId;

 public class InstantToLocalDateTime {
  public static void main(String[] args) {
    Instant instant = Instant.now();
    System.out.println(instant);

    // Using LocalDateTime.ofInstant() method
    LocalDateTime time =
      LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    System.out.println(time);

    // Using Timestamp.from(Instant).toLocalDateTime()
    LocalDateTime time1 =
      Timestamp.from(instant).toLocalDateTime();
    System.out.println(time1);
  }
 }

Output:

2020-07-22T13:15:10.058Z
2020-07-22T18:45:10.058
2020-07-22T18:45:10.058

Conclusion:

In this tutorial, we have covered how we can convert LocalDateTime to Instant and Instant to LocalDate in Java using different methods.

Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.

Happy Learning!!!

References:

Comments