首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用LocalDateTime实现Java转换

问用LocalDateTime实现Java转换
EN

Stack Overflow用户
提问于 2020-02-06 05:36:25
回答 1查看 3.4K关注 0票数 4

环境

  • 紫花莲-弹簧-启动-启动器2.0.1
  • 紫花莲3.5.1
  • 紫花莲-春季2.0.1
  • mysql-连接器-java 8.0.16

jvm时区:亚洲/首尔

我的测试查询

SELECT #{from}, #{to}

输入

代码语言:javascript
复制
YearMonth input = YearMonth.of(year, month)
LocalDateTime from = input.atDay(1).atStartOfDay()
LocalDateTime to = input.atEndOfMonth().atTime(LocalTime.MAX)

Case1。将serverTimezone=UTC添加到jdbc url中

Case1。将serverTimezone=Asia/Seoul添加到jdbc url中

代码语言:javascript
复制
[11:03:54] [main] [DEBUG] BaseJdbcLogger.debug(143)| ==>  Preparing: SELECT ?, ? 
[11:03:54] [main] [DEBUG] BaseJdbcLogger.debug(143)| ==> Parameters: 2020-01-01T00:00(LocalDateTime), 2020-01-31T23:59:59.999999999(LocalDateTime)
[11:03:54] [main] [DEBUG] Slf4jSpyLogDelegator.sqlTimingOccurred(368)|  com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
1. SELECT 2020-01-01T00:00, 2020-01-31T23:59:59.999999999
 {executed in 3 msec}
[11:03:54] [main] [INFO ] Slf4jSpyLogDelegator.resultSetCollected(610)| 
|----------------------|----------------------|
|2019-12-31 15:00:00.0 |2020-01-31 15:00:00.0 |
|----------------------|----------------------|
|----------------------|----------------------|
代码语言:javascript
复制
[14:27:16] [main] [DEBUG] BaseJdbcLogger.debug(143)| ==>  Preparing: SELECT ?, ? 
[14:27:16] [main] [DEBUG] BaseJdbcLogger.debug(143)| ==> Parameters: 2020-01-01T00:00(LocalDateTime), 2020-01-31T23:59:59.999999999(LocalDateTime)
[14:27:16] [main] [DEBUG] Slf4jSpyLogDelegator.sqlTimingOccurred(368)|  com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
1. SELECT 2020-01-01T00:00, 2020-01-31T23:59:59.999999999
 {executed in 4 msec}
[14:27:16] [main] [INFO ] Slf4jSpyLogDelegator.resultSetCollected(610)| 
|----------------------|----------------------|
|2020-01-01 00:00:00.0 |2020-02-01 00:00:00.0 |
|----------------------|----------------------|
|----------------------|----------------------|

Q1。java LocalDateTime是否被LocalDateTimeTypeHandler转换为jdbc时间戳?

Q2。LocalDateTimeTypeHandler是否基于jdbc设置转换值?

Q3。为什么我的2020-01-31T23:59:59.999999999值转换为2020-02-01 00:00:00.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-06 13:35:57

  • A1。不,MyBatis从3.5.1版本开始就没有将LocalDateTime转换为java.sql.Timestamp (不过在早期版本中是这样的)。
  • A2。不,MyBatis只是将值传递给驱动程序。您可以自己检查类型处理程序实现。 实际上,执行转换的是驱动程序,该行为在其跟踪器上作为bug被归档。 https://bugs.mysql.com/bug.php?id=93444
  • A3。而Java的LocalDateTime具有纳秒(9位)的精度,而MySQL的TIME、DATETIME和TIMESTAMP的精度只有微秒(6位)。你观察到的行为在MySQL文档中得到了解释。将带有小数秒部分的时间、日期或时间戳值插入同一类型的列,但小数位数较少,则会导致舍入。
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60088304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档