Java Data Type How to - Convert LocalDateTime to java.sql.Timestamp








Question

We would like to know how to convert LocalDateTime to java.sql.Timestamp.

Answer

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
//from w w w. j  a  va  2 s .c o  m
public class Main {

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

  public static Timestamp timeZoneAdjustedDate(LocalDateTime due) {
    return Timestamp.from(due.toInstant(ZoneOffset.ofHours(0)));
  }

}

The code above generates the following result.