Java Data Type How to - Convert LocalDate to a java.sql.TimeStamp








Question

We would like to know how to convert LocalDate to a java.sql.TimeStamp.

Answer

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
/*from w ww.j ava 2  s . c  o  m*/
public class Main {

  public static void main(String[] args) {
    System.out.println(toTimestamp(LocalDate.now()));
  }

  public static Timestamp toTimestamp(LocalDate localDate) {
    Date date = Date.from(localDate.atStartOfDay()
        .atZone(ZoneId.systemDefault()).toInstant());
    Timestamp timeStamp = new Timestamp(date.getTime());
    return timeStamp;
  }
}

The code above generates the following result.