java判断字符串是否为指定的日期格式
正则复杂得有点过分,异常判断又看着不爽,换了一种写法。
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test97 {
public static void main(String[] args) {
System.out.println(isDate("2019-12-34", "yyyy-mm-dd"));
}
private static boolean isDate(String dat, String fmt) {
DateFormat formatter = new SimpleDateFormat(fmt);
ParsePosition pos = new ParsePosition(0);
formatter.setLenient(false);
Date result = formatter.parse(dat, pos);
return !(pos.getIndex() == 0) && dat.equals(formatter.format(result));
}
}
还没有评论,来说两句吧...