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








Question

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

Answer

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
//ww w .  j  av  a2 s.  c  o  m
public class Main {

  public static void main(String[] args) {

    System.out.println(toZonedDateTime(new Date()));
  }

  public static ZonedDateTime toZonedDateTime(Date utilDate) {
    if (utilDate == null) {
      return null;
    }
    final ZoneId systemDefault = ZoneId.systemDefault();
    return ZonedDateTime.ofInstant(utilDate.toInstant(), systemDefault);
  }
}

The code above generates the following result.