Converting a Date to an Instant and Vice Versa - Java Date Time

Java examples for Date Time:Instant

Description

Converting a Date to an Instant and Vice Versa

Demo Code

import java.util.Date;
import java.time.Instant;

public class Main {
  public static void main(String[] args) {
    // Get the current date
    Date dt = new Date();
    System.out.println("Date: " + dt);

    // Convert the Date to an Insatnt
    Instant in = dt.toInstant();/*from   w w w  .  j ava2 s . c  o  m*/
    System.out.println("Instant: " + in);

    // Convert the Instant back to a Date
    Date dt2 = Date.from(in);
    System.out.println("Date: " + dt2);
  }
}

Related Tutorials