[英]Spring myBatis forcing to save ZonedDateTime in system default timezone, how to prevent?
I am setting up an API that receives datetimes as String alongside with the country in which the datetimes are from. 我正在设置一个API,该API接收日期时间为String以及日期时间来自的国家/地区。 I have a formatter that converts those strings into ZonedDateTime
我有一个将这些字符串转换为ZonedDateTime的格式化程序
Say I am sending this JSON: 假设我正在发送此JSON:
{
"country": "ID",
"activeUntil":"2020-03-10 08:00:00"
}
ID is for Indonesia (Timezone "Asia/Jakarta" UTC+7) ID适用于印度尼西亚(时区“亚洲/雅加达” UTC + 7)
This is my converter 这是我的转换器
public static ZonedDateTime convertDatetimeStringToDatetime(String dtString, String country) throws ParseException {
DateFormat formatter = new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"));
formatter.setTimeZone(TimeZone.getTimeZone(CountryTimezones.timezoneMap.get(country)));
return formatter.parse(dtString).toInstant().atZone(ZoneId.of(CountryTimezones.timezoneMap.get(country)));
}
CountryTimezones.getTimeZone is just a Map that link a country shortcode to its timezone. CountryTimezones.getTimeZone只是将国家/地区简码链接到其时区的地图。
Until this point all goes alright, my object holds the value that I want. 直到一切顺利,我的对象才拥有我想要的值。
log.info(myObject.getActiveUntil().toString());
log.info(myObject.getActiveUntil().toLocalDateTime().toString());
log.info(myObject.getActiveUntil().withZoneSameInstant(ZoneId.of("UTC")).toString());
shows 表演
2020-03-10T08:00+07:00[Asia/Jakarta]
2020-03-10T08:00
2020-03-10T01:00Z[UTC]
The problems come when I persist the date into database. 当我将日期保留到数据库中时,问题就来了。 I am using mybatis, and my query looks like this:
我正在使用mybatis,查询如下:
<insert id="insert">
INSERT INTO sometable (country, active_until, created_by)
VALUES (#{entity.country}, #{entity.activeUntil}, #{entity.createdBy})
</insert>
(shortened for clarity) (为清楚起见已缩短)
And the datetime saved is "2020-03-10 09:00:00" 保存的日期时间是“ 2020-03-10 09:00:00”
For information my location timezone is utc+8, meaning, the datetime is autoconverted to my location timezone. 有关信息,我的位置时区为utc + 8,即日期时间已自动转换为我的位置时区。
MyBatis version <mybatis.version>3.4.5</mybatis.version>
MyBatis版本
<mybatis.version>3.4.5</mybatis.version>
I am using MySQL DB 我正在使用MySQL DB
The type of the active_until column is (MySQL) datetime. active_until列的类型为(MySQL)datetime。
This the Query Log : 这查询日志:
==> Preparing: INSERT INTO sometable (country, active_until, created_by) VALUES (?, ?, ?)
==> Parameters: ID(String), 2020-03-10 09:00:00.0(Timestamp), Created by System(String)
How to change this behavior to force save all my ZonedDateTime in UTC 如何更改此行为以强制将我的所有ZonedDateTime保存在UTC中
I fixed it by adding this to my Application.java 我通过将其添加到Application.java中来修复它
@PostConstruct
public void init() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.out.println("Spring boot application running in UTC timezone :" + new Date());
}
myBatis apparently takes the timezone of the application as the timezone to use for saving a ZonedDateTime. myBatis显然将应用程序的时区用作保存ZonedDateTime的时区。 Since I hadn't set it up, the application timezone was using system default which must have been the timezone of my location.
由于尚未设置,因此应用程序时区使用的是系统默认值,该时间必须是我所在位置的时区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.