Java - Convert java.time.Instant to java.sql.Timestamp without Zone offset

前端 未结 4 1628
遥遥无期
遥遥无期 2020-11-30 03:42

In the application I am developing, I need to convert java.time.Instant object to java.sql.Timestamp. When I create Instant object lik

4条回答
  •  一生所求
    2020-11-30 04:21

    During saving a record to SQL Server DB I faced with the same problem. I've used java.sql.Timestamp.valueOf(String s) to get Timestamp in UTC:

    import java.time.Instant; import java.time.format.DateTimeFormatter; .... .... DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(UTC); String dateTime = dateTimeFormatter.format(Instant date); Timestamp timestamp = Timestamp.valueOf(dateTime);

    It works for me.

提交回复
热议问题