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

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

 
   	 	org.apache.commons
    	commons-lang3
    	3.9
  

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(), time);
			System.out.println(oid.intValue());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
``
	

你可能感兴趣的:(javaee)