[总结]mysql 插入dateTime 2020.01.13 23:59:59 最终值是2020.01.14 00:00:00

20 篇文章 1 订阅
2 篇文章 0 订阅

问题描述

今天发现一个奇怪的现象,在笔者的某个业务场景中,需要存储一个时间,对于时间的存储,我们一般期望开始时间是2020.01.13 00:00:00 结束时间我们期望是2020.01.13 23:59:59

但是在实际存储却变成了2020.01.14 00:00:00

于是经过一番debug,终于定位到问题。

排查分析

生成指定日期最大时间点代码

/**
 * 获得某天最大时间 2017-10-15 23:59:59.999
 * @param date
 * @return
 */
public static Date getEndTimeOfDay(Date date) {
    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
    LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
    endOfDay.withNano(0);
    return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
}

insert语句

[EAP] 2020-01-13 16:36:38.713 [DEBUG] [DubboServerHandler-10.10.30.94:20884-thread-199] com.jlb.eap.vacation.dao.VacationInfoMapper.insert.debug:143 - ==>  Preparing: INSERT INTO eap_vacation_info ( id,lesson_id,start_time,end_time,type,vacation_value,learner_id,class_id,gmt_create,gmt_modified,real_end,status,real_value,vacation_reason,teacher_uid,operator_uid,curriculum_id,extend,deleted,creator,modifier,request_source,role_type ) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )

 

[EAP] 2020-01-13 16:36:38.714 [DEBUG] [DubboServerHandler-10.10.30.94:20884-thread-199] com.jlb.eap.vacation.dao.VacationInfoMapper.insert.debug:143 - ==> Parameters: 15163469(Long), null, 2020-01-13 00:00:00.0(Timestamp), 2020-01-13 23:59:59.999(Timestamp), 0(Integer), 2(Integer), 84971597(Long), 26705989(Long), 2020-01-13 16:36:23.848(Timestamp), 2020-01-13 16:36:24.521(Timestamp), null, 0(Integer), null, 1(Integer), 69d90209551c422a82daec4d898a0d85(String), 6248756da3504c9c9638b75d789bb509(String), 410931277(Long), {"reason":"0","reasonDes":"测试请假时间"}(String), 0(Integer), 10313668(Long), 10313668(Long), 0(Integer), 1(Integer)

注意日志中:

 2020-01-13 00:00:00.0(Timestamp), 2020-01-13 23:59:59.999(Timestamp)

 

实际存储到mysql后变为

 

修复后

 

 

解决问题

/**

 * 获得某天最大时间 2017-10-15 23:59:59.000

 * @param date

 * @param withZeroNano  等于true 2017-10-15 23:59:59.000 ;等于false = 2017-10-15 23:59:59.999

 * @return

 */

public static Date getEndTimeOfDay(Date date, boolean withZeroNano) {

    LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());

    LocalTime localTime = null;

    if (withZeroNano){

        localTime = LocalTime.of(235959);

    }else {

        localTime = LocalTime.MAX;

    }

    LocalDateTime endOfDay = localDateTime.with(localTime);

    return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长乐smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值