万能处理方案:
所有日期格式都可以通过getDataFormat()值来判断
yyyy-MM-dd----- 14
yyyy年m月d日--- 31
yyyy年m月------- 57
m月d日 ---------- 58
HH:mm----------- 20
h时mm分 ------- 32
//1、判断是否是数值格式
if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
short format = cell.getCellStyle().getDataFormat();
SimpleDateFormat sdf = null;
if(format == 14 || format == 31 || format == 57 || format == 58){
//日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else if (format == 20 || format == 32) {
//时间
sdf = new SimpleDateFormat("HH:mm");
}
double value = cell.getNumericCellValue();
Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(value);
result = sdf.format(date);
}
以上是借鉴他人博客,下面是我的解决方法
//判断是否是数值格式
if(row.getCell(4).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
short format = row.getCell(4).getCellStyle().getDataFormat();
//判断日期个格式是否是 2017/01/01 这样
/*
* 14 yyyy-MM-dd / 2017/01/01
* 31 yyyy年m月d日
* */
if(format == 14 || format == 31){
Date date = HSSFDateUtil.getJavaDate(row.getCell(4).getNumericCellValue());
}