Java Program to convert java.util.Date to Instant


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements