Java LocalDate convert to Date

Description

Java LocalDate convert to Date

import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;

public class Main {
  public static void main(String[] argv) {
    LocalDate ld = LocalDate.now();
    Date d = localDateToDate(ld);

    System.out.println(ld);//from w  ww. ja v  a2s  .c  om
    System.out.println(d);
  }

  public static Date createDate(int year, Month month, int day) {
    LocalDate localDate = LocalDate.of(year, month, day);
    return localDateToDate(localDate);
  }

  public static Date localDateToDate(LocalDate localDate) {
    return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
  }
}



PreviousNext

Related