将excel数据写入数据库,其中excel中有日期,时间,数字,字符等,如何将时间写入
其中判断为数据时代码为
case NUMERIC:
if (DateUtil.isCellDateFormatted(hssfCell)) {
Date d = hssfCell.getDateCellValue();
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
cellValue = df2.format(d);
}
// 数字
else {
cellValue = (String.valueOf(hssfCell.getNumericCellValue())) ;
}
break;
case BOOLEAN:
cellValue = String.valueOf(hssfCell.getNumericCellValue());
break;
time的get set方法的代码为
private String time;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
现在其他的都解决了,就是时间这部分该怎么处理,麻烦各位大佬了。
Excel导入数据,时间也是按字符串去取出来,只是在取出来之后,再进行字符串转时间,下面是我的写的逻辑,可以参考一下
String timeStr = sheet.getCell4,1).getContents() + " " + sheet.getCell(5, 1).getContents();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
date = format.parse(timeStr);
//然后把date拿来存到数据库就行了