首页 > 其他 > 详细

JXL读取Excel日期时间不准确

时间:2015-09-09 21:11:50      阅读:300      评论:0      收藏:0      [点我收藏+]

XL读取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日期时间不准确

原文:http://www.cnblogs.com/toSeeMyDream/p/4795679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!