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;
}