校验年月日格式Java
时间: 2023-10-21 12:06:08 浏览: 101
Java正则校验日期格式包括所有
Sure, I can help with that! To validate a date in Java, you can use the SimpleDateFormat class. Here's an example:
String inputDate = "2022-01-31";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setLenient(false);
try {
Date date = sdf.parse(inputDate);
System.out.println("Valid date!");
} catch (ParseException e) {
System.out.println("Invalid date!");
}
This code takes a date string in the format "yyyy-MM-dd" and tries to parse it using the SimpleDateFormat object. If the date is valid, it prints "Valid date!" to the console; otherwise, it prints "Invalid date!". I hope this helps!
阅读全文