/**
* 转换日期格式为年月日时分秒/年月日自动补齐时分秒
* @param value
* @return
* @throws ParseException
*/
public static String extendDate(Object value) {
SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String type = value.getClass().toString();
String time = “”;
if(type.equals(“class java.time.LocalDateTime”)){
if(value != null){
//转为datetime类型
Date date = Date.from(((LocalDateTime) value).toInstant(ZoneOffset.of("+8")));
time = formatter.format(date);
}
}
if(type.equals("class java.lang.String")){
String strvalue = value.toString();
if(value != null){
try {
if(ValidDate(strvalue)){
Date date;
date = formatter.parse(strvalue.toString());
time = formatter.format(date);
}else{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(strvalue);
time = formatter.format(date);
}
} catch (ParseException e) {//如果不能转就不转
time = strvalue;
}
}
}
return time;
}
/**
* 校验字符串是不是年月日时分秒格式的
* @param str
* @return
*/
public static boolean ValidDate(String str) {
boolean convertSuccess=true;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
convertSuccess=false;
}
return convertSuccess;
}