poi 读取excle文件时日期转换问题,渴望大神帮忙解决一下

private String getValue (HSSFCell hssfCell) {
        switch (hssfCell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC:
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/mm/dd");
                String cellValue = "";
                if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                    System.out.println("hssfCell="+ hssfCell.getNumericCellValue());

                    Double d = hssfCell.getNumericCellValue();
                    System.out.println("d="+ d);


                    Date date = HSSFDateUtil.getJavaDate(d);
                    System.out.println("date=" + sdf.format(date));

                    cellValue = sdf.format(date);
                    System.out.println("cellValue=" + cellValue);
                }
                else{
                    DecimalFormat df = new DecimalFormat("#.####");
                    cellValue = df.format(hssfCell.getNumericCellValue());
                }
                return cellValue;
            case HSSFCell.CELL_TYPE_STRING:
                return hssfCell.getStringCellValue();
            case HSSFCell.CELL_TYPE_FORMULA:
                return hssfCell.getCellFormula();
            case HSSFCell.CELL_TYPE_BLANK:
                return "";
            case HSSFCell.CELL_TYPE_BOOLEAN:
                return String.valueOf(hssfCell.getBooleanCellValue());
        }
        return "";
    } 

输出:

  • hssfCell=42095.0
  • d=42095.0
  • date=2015/00/01
  • cellValue=2015/00/01
  • 问题:
  • double 类型的数字转换成日期是出错 数字转化成日期是只能正确显示年份,月份和天数都是错的
  • 出错的是代码片中 Date date = HSSFDateUtil.getJavaDate(d);

http://yl-fighting.iteye.com/blog/1726285