JXL读取Excel日期时间多出了8个小时。
Cell c = rs.getCell(j, i);
if (c.getType() == CellType.DATE) {//手动填写模板文件时为 date 类型,其他情况有可能不是date类型
DateCell dc = (DateCell) c;
Date date = dc.getDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = sdf.format(date);
nextLine[j] = sDate;
}
else {
String d = rs.getCell(j, i).getContents().trim();
nextLine[j] = d;
}
解决办法:获取的日期时间需要调整时区。参见:http://www.andykhan.com/jexcelapi/tutorial.html#dates
Cell c = rs.getCell(j, i);
if (c.getType() == CellType.DATE) {//手动填写模板文件时为 date 类型,其他情况有可能不是date类型
DateCell dc = (DateCell) c;
Date date = dc.getDate();
TimeZone zone = TimeZone.getTimeZone("GMT");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(zone);
String sDate = sdf.format(date);
nextLine[j] = sDate;
}
else {
String d = rs.getCell(j, i).getContents().trim();
nextLine[j] = d;
}
本文介绍使用JXL库读取Excel文件时遇到的日期时间显示偏差问题及解决方案。通过设置日期时间的时区为GMT,可以有效解决读取到的时间比实际时间多出8小时的问题。
1191

被折叠的 条评论
为什么被折叠?



