public class StringParseDate {
/**
* 判断字符串值是否为空 true:空 false:非空
* @param value
* @return
*/
public static boolean isEmpty(String value){
if(value == null || "".equals(value)){
return true;
}
return false;
}
/**
* 判断字符串是否是日期格式 true:是 false:不是
* @param value
* @param format
* @return
*/
public static boolean isDate(String value,String format){
SimpleDateFormat sdf = null;
//指定从所传字符串的首位开始解析
ParsePosition pos = new ParsePosition(0);
if(value == null || isEmpty(format)){
return false;
}
try {
sdf = new SimpleDateFormat(format);
sdf.setLenient(false);
Date date = sdf.parse(value,pos);
if(date == null){
return false;
}else{
//更为严谨的日期格式校验,如2016-11-023认为是不合法的
if(pos.getIndex() > sdf.format(date).length()){
return false;
}
return true;
}
}catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
String转为Date类型
最新推荐文章于 2023-05-24 11:55:14 发布