/**
* 判断日期的格式:
*
* @param timeStr
* @return
*/
public static String getDateFormat(String timeStr) {
String regex0="\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}.\\d{3}";
String regex1="\\d{4}-\\d{2}-\\d{2}\\s{1}\\d{2}:\\d{2}:\\d{2}";
String regex2="\\d{4}-\\d{2}-\\d{2}";
// 编译正则表达式
Pattern pattern0=Pattern.compile(regex0);
Pattern pattern1=Pattern.compile(regex1);
Pattern pattern2=Pattern.compile(regex2);
// 忽略大小写的写法
// Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
Matcher matcher0=pattern0.matcher(timeStr);
Matcher matcher1=pattern1.matcher(timeStr);
Matcher matcher2=pattern2.matcher(timeStr);
if(matcher0.matches()) {
return "yyyy-MM-dd HH:mm:ss.SSS";
}
if(matcher1.matches()) {
return "yyyy-MM-dd HH:mm:ss";
}
if(matcher2.matches()) {
return "yyyy-MM-dd";
}
return null;
}
参考: https://blog.csdn.net/qq_41307443/article/details/79752682.