我正在使用网络门户的上类/下类功能,该门户可以由不同时区的终端访问。
我正在使用 ZonedDateTime 来计算给定打卡/打出记录的日期时间。
// Get store timezone
ZoneId storeTimeZone = this.storeService.getStoreZoneId(storeNum);
// Get current store time to use for punch in / out timestamp
ZonedDateTime currentStoreTime = ZonedDateTime.ofInstant(Instant.now(), storeTimeZone);
当我将数据保存到 Oracle 11g 时,我无法保存 ZonedDateTime 对象。 JDBC 驱动程序可能不支持。它是企业环境,因此数据库和驱动程序不会改变。
当我构建 MapSqlParamSource 以传递到 NamedParameterJdbcTemplate 时,我必须将其转换为 java.sql.Timestamp 但随后我会丢失区域数据并存储服务器所在时区的时间戳,需求决定了它必须存储在数据库中的存储时间中,该数据库只是一个 Oracle TIMESTAMP 列。
params.addValue("clockInTimestamp", Timestamp.from(prevRecord.getTimeEntryTimestamp().toInstant()), Types.TIMESTAMP);
有没有办法实现这个持久的ZonedDateTime?
最佳答案
我设法为 MySQL 修复了这个问题,因此它可能也适用于其他人。我在将 sqlType
添加到 MapSqlParameterSource
ZonedDateTime zdt = ZonedDateTime.now();
LocalDateTime ldt = LocalDateTime.ofInstant(zdt.toInstant(), ZoneId.systemDefault());
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("datetime", ldt, Types.JAVA_OBJECT);
关于java - Spring JDBC 持久化 ZonedDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46818888/