`
wandejun1012
  • 浏览: 2693436 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

jxl读取excel日期相差8小时

    博客分类:
  • java
 
阅读更多

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 germmy@20120711 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 germmy@20120711 end--------------------------
 

 

 

 

refurl:

http://blog.csdn.net/jeamking/article/details/7288353

 

http://www.oschina.net/question/112251_31697

 

 

分享到:
评论
1 楼 代码改变生活 2017-07-11  
虽然代码敲的头疼的要命,但是依然要感谢你,感谢您共享!!!!开源的就是好

相关推荐

Global site tag (gtag.js) - Google Analytics