Java Data Type How to - Convert Instant to java.util.Date








Question

We would like to know how to convert Instant to java.util.Date.

Answer

import java.math.BigInteger;
import java.time.Instant;
import java.util.Date;
/*from  w  w  w  .  ja  v a  2s .  co  m*/
public class Main {

  public static void main(String[] args) {
    Instant i = Instant.now();

    System.out.println(toDate(i));

  }

  public static Date toDate(Instant instant) {
    BigInteger milis = BigInteger.valueOf(instant.getEpochSecond()).multiply(
        BigInteger.valueOf(1000));
    milis = milis.add(BigInteger.valueOf(instant.getNano()).divide(
        BigInteger.valueOf(1_000_000)));
    return new Date(milis.longValue());
  }
}

The code above generates the following result.