Is java.sql.Timestamp timezone specific?

后端 未结 6 1545
忘掉有多难
忘掉有多难 2020-11-22 04:51

I have to store UTC dateTime in DB.
I have converted the dateTime given in specific timezone to UTC. for that I followed the below code.
My input dateTime is \"20121

6条回答
  •  不知归路
    2020-11-22 05:09

    You can use the below method to store the timestamp in database specific to your desired zone/zone Id.

    ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("Asia/Calcutta")) ;
    Timestamp timestamp = Timestamp.valueOf(zdt.toLocalDateTime());
    

    A common mistake people do is use LocaleDateTime to get the timestamp of that instant which discards any information specif to your zone even if you try to convert it later. It does not understand the Zone.

    Please note Timestamp is of the class java.sql.Timestamp.

提交回复
热议问题