`
kinkding
  • 浏览: 147918 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JXL时间类型处理

    博客分类:
  • JAVA
阅读更多

JXL对于日期类型的处理,有些特殊需要注意:

 

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.JXLException;
import jxl.Sheet;
import jxl.Workbook;

public class Test {
	public static void main(String[] args) {
		Test t = new Test();
		try {
			t.read();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void read() throws JXLException, IOException {
		Workbook wb = Workbook.getWorkbook(new File("input2.xls"));
		Sheet sheet = wb.getSheet(0);
		for (int i = 0, rowSize = sheet.getRows(); i < rowSize; i++) {
			for (Cell c : sheet.getRow(i)) {
				CellType type = c.getType();

				if (type.equals(CellType.DATE)) {
					// 日期 类型的处理
					DateCell dc = (DateCell) c;
					Date jxlDate = dc.getDate();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
					System.out.println(sdf.format(jxlDate));
				}
			}
		}
		wb.close();
	}
}

 

比较特别就是format的时候,必须采用标准的GMT时区,否则就会出错了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics