首页 > 学院 > 开发设计 > 正文

jxl读取excel中Date类型

2019-11-08 02:29:42
字体:
来源:转载
供稿:网友

关键问题:

        1.使用DateCell类型读取cell

        2.读取出来的时间可能需要修改时区

读取Excel中的日期

           

          如图,读取K列1行数据

                    代码如下:
		Cell cell;		DateCell dc;		String str = "";		Date date;		SimpleDateFormat SF_date = new SimpleDateFormat("yyyy-M-d");		SimpleDateFormat SF_time = new SimpleDateFormat("HH:mm:ss");
				cell = sheet.getCell(10,0);			dc = (DateCell)cell;		date = dc.getDate();		str = SF_date.format(date);		System.out.PRintln(str);		str = SF_time.format(date);		System.out.println(str);

            输出结果如下:

                                                                                   可以看出,日期的格式是正确的,但是数值不正确,恰好比实际的时间多出了8个小时。修正的方法是添加修改时区的代码。

            几点说明:

                         1.excel中时间类型的cell需要用datecell读取,将cell强制转为datecell类型后,用datecell的getDate方法得到date类型的数据。

                          2.使用simpleDateFormat来指定日期的格式,代码如上。

                          3.用到的几个包:

                                     import java.text.SimpleDateFormat;                                     import java.util.Date;

                                     import jxl.Cell;

                                     import jxl.DateCell;

修改时区

                         修改时区,需要修改代码为:
		Cell cell;		DateCell dc;		String str = "";		Date date;		SimpleDateFormat SF_date = new SimpleDateFormat("yyyy-M-d");		SimpleDateFormat SF_time = new SimpleDateFormat("HH:mm:ss");		TimeZone zone = TimeZone.getTimeZone("GMT");				cell = sheet.getCell(X_Axi,0);		dc = (DateCell)cell;		date = dc.getDate();		SF_date.setTimeZone(zone);		SF_time.setTimeZone(zone);		str = SF_date.format(date);		System.out.println(str);		str = SF_time.format(date);		System.out.println(str);                        输出结果为:                                                       
上一篇:说明符

下一篇:MySQL日志系统

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表