这个bug发生在使用poi组件导入导出excel时,(这里是导入)
首先在excel中的格式设定是
yyyy-mm-dd hh:mm:ss
通过配套使用ExcelUtil中 getCellValue(Cell cell)获取单元格的类型
经过判定后,时间格式会进入:
该getCellValue(Cell cell)返回类型为Object
cellValue = null;
case NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)){
cellValue = cell.getDateCellValue();
}
这种获取时间的方式会少8个小时.
经过查看poi的DateUtil源码改为:
switch (cell.getCellType()) { case NUMERIC: // 数字 if (HSSFDateUtil.isCellDateFormatted(cell)) { double numericValue = cell.getNumericCellValue(); TimeZone zone = TimeZone.getTimeZone("GMT"); Date javaDate = DateUtil.getJavaDate(numericValue, zone); System.out.println("javaDate:"+javaDate); cellValue = javaDate; } else
异常消失,时间正常.
所以采用了上面设置timezone.并通过DateUtil(poi的)getJavaDate传入该zone即可.
这里可以将bean实体类上的时间GMT+8:00给取消了,参考: https://www.cnblogs.com/ukzq/p/9937002.html