Java LocalDateTime convert from Date

Description

Java LocalDateTime convert from Date

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

import java.time.ZoneId;

import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    /* ww  w . j a v  a 2  s. com*/
    LocalDateTime ldt = dateToLocalDateTime(new Date()); 
     System.out.println(ldt);
  }

  /**
   * Converts the specified Date object to LocalDateTime.
   *
   * @param date
   *          Date object containing the date and time
   * @return the created LocalDateTime (JSR 310)
   */
  public static LocalDateTime dateToLocalDateTime(final Date date) {
    final Instant instant = Instant.ofEpochMilli(date.getTime());
    return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
  }
}



PreviousNext

Related