关于mysql数据库因为对事件类型进行四舍五入导致时间比实际值快1秒的解决办法

本文介绍如何使用org.apache.commons.lang3.time.DateUtils包将时间精度精确到秒级,通过具体示例展示了如何在Java中应用此库来避免数据库时间类型的四舍五入问题,确保时间戳的准确性。

1.将时间精度精确到秒级
1)引入org.apache.commons.lang3.time.DateUtils包,下载网址https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.9
或者导入下面的maven库
dateutil(引入原因:解决mysql数据库对时间类型进行四舍五入导致时间比实际值快一秒)

 <dependency>
   	 	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-lang3</artifactId>
    	<version>3.9</version>
  </dependency>

2)junit测试用例

@Test
	public void test3(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = DateUtils.round(new Date(), Calendar.SECOND);//将时间精度精确到秒级
		String time = sdf.format(date);
		System.out.println(time);
		try {
			QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
			String sqlInsert = "insert into orders(ordertime) values(?)";
			qr.update(sqlInsert, date);
			String sql = "select oid from orders where ordertime=?";
			Long oid = qr.query(sql, new ScalarHandler<Long>(), time);
			System.out.println(oid.intValue());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
``
	
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值