Java Program to convert java.util.Date to Instant

Java 8Object Oriented ProgrammingProgramming

First, set the date −

java.util.Date date = new java.util.Date();

Now, convert the above date to Instant −

Instant instant = date.toInstant();

Example

import java.time.Instant;
public class Demo {
   public static void main(String[] args) {
      java.util.Date date = new java.util.Date();
      System.out.println("Date = "+date);
      Instant instant = date.toInstant();
      System.out.println("java.util.Date to Instant = "+instant);
   }
}

Output

Date = Thu Apr 18 23:32:07 IST 2019
java.util.Date to Instant = 2019-04-18T18:02:07.330Z
raja
Published on 26-Apr-2019 10:18:44
Advertisements