SimpleDateFormat类的parse方法可以帮我们判断是否是合法日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
// boolean is_time(String t) {
// Pattern pattern = Pattern.compile("[0-9]{4}[-][0-1]{1}[0-9]{1}[-][0-3]{1}[0-9]{1}[ ][0-9]{2}[:][0-9]{2}[:][0-9]{2}");
// Matcher matcher = pattern.matcher(t);
//
// if (matcher.find()){
// return true;
// }
// return false;
// }
public static void main(String[] args) {
boolean convertSuccess=true;
String t = "2020-02-29 10:22:34";
//
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
format.setLenient(false);
format.parse(t);
} catch (ParseException e){
convertSuccess = false;
}
System.out.println(convertSuccess);
}
}