Reading Excel Date fields from Java

Previously I posted on how to read data from Microsoft Excel file using Apache POI HSSF. This is a small addition to that.

If the Excel file you are to read is using 'Date' fields, then you have to be little careful about it. In HSSF these Date fields are taken as numeric fields. Anyway that is how Dates are handled in Excel; it uses an Integer to keep number of days since 1900-Jan-0. Luckily the there is a small utility provided by HSSF to covert these Execl Dates to Java Date format. Just use HSSFDateUtil.getJavaDate(double) method.

//Date of Birth
HSSFRow row = sheet.getRow(ROW_C_DOB);
//Handle Excel Date type cells
HSSFCell cell = row.getCell(COLUMN_DATA);
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
  Date d = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
  String tmp =    row.getCell(COLUMN_DATA).getRichStringCellValue().toString();

Reader Comments

Great post, helped me a lot!

Tribune to Heros

Tribune to Heros
We will never forget the sacrifices you made to protect our country....


Get Google Toolbar Button

Enter your email address:

Delivered by FeedBurner