java读取excel里面的日期会出现相差8小时的问题。
比如excel里面有一个日期是:2012-7-2 17:14:03秒,用Cell cell=readSheet.getCell(colNo, rowNo);调试该cell,发现里面的值竟然是2012-7-3 1:14:13,相差了8小时。
更奇怪的是,用String date=cell.getContents()后,得到的值竟然是2012-7-2 5:14:03分了,将24小时制变成了12小时制了。
原因是:
1、对于Date型的单元格,不应该用cell.getContents(),应该用别的方法,见如下示例。
2、还有一个就是时区问题,要用GMT时区。
具体参见以下示例:
Cell cell = readSheet.getCell(colNo, rowNo);
String cellValue=null;
//----------------mod by [email protected] start-------------------------
if (cell.getType() == CellType.DATE)
{
DateCell dc = (DateCell) cell;
Date date = dc.getDate();
TimeZone zone = TimeZone.getTimeZone("GMT");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(zone);
cellValue = sdf.format(date);
} else{
cellValue = cell.getContents();
}
//----------------mod by [email protected] end--------------------------
refurl:
http://blog.csdn.net/jeamking/article/details/7288353
http://www.oschina.net/question/112251_31697