现在需要将数据库时间通过poi导出,但是导出时间时,出现的时间格式不需要;
解决思路:将时间转成string,在poi导出
1.格式化时间
public static String dateFormatter(Object obj){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (obj==null) {
return null;
}else {
Date date=null;
if (obj instanceof String) { //属于string
try {
date = sdf.parse(obj.toString());
String format = sdf.format(date);
return format;
} catch (Exception e) {
return null;
}
}else if (obj instanceof Date) { // 属于date
date=(Date) obj;
String format = sdf.format(date);
return format;
}
return null;
}
}
上面的代码判断类型,是因为我这边的数据时object类型,判断obj的实际类型在导出(instance of)