如何使用poi从excel读取日期和时间值?

时间:2015-07-30 11:48:31

标签: java apache-poi

我必须从excel文件中获取数据。这是数据

领取日期24-Oct-15

送出日期01-Nov-15

接送时间09:20:00

下午11:05上午

以下是我使用从excel获取'Drop off date'值的代码。

    String xlfile = "C:\\Test_data.xlsx";
    FileInputStream file = new FileInputStream(new File(xlfile));
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    XSSFSheet sheet = workbook.getSheetAt(0);
    Cell cell = null;
    cell = sheet.getRow(4).getCell(1);
    if (cell.getCellType()== Cell.CELL_TYPE_STRING)
    {
        System.out.println(cell.getStringCellValue());
    }
    else if (cell.getCellType()== Cell.CELL_TYPE_NUMERIC)
    {
    System.out.println(cell.getNumericCellValue());
    }

我得到的值为42309.0,而我应该得到的值为01-Nov-15。我也尝试使用cell.getDateCellValue()但是使用该选项,我得到的值是Sun Nov 01 00:00:00 IST 2015.

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

感谢您的帮助!下面的代码对我有用。

DataFormatter df = new DataFormatter(); 
df.formatCellValue(cell)