思路很简单,格式化时间。
public static boolean checkHHMM(String time) {
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm");
try {
@SuppressWarnings("unused")
Date t = dateFormat.parse(time);
} catch (Exception ex) {
return false;
}
return true;
}
public static boolean checkTime(String time) {
if (checkHHMM(time)) {
String[] temp = time.split(":");
if ((temp[0].length() == 2 || temp[0].length() == 1) && temp[1].length() == 2) {
int h = 0;
int m = 0;
try {
h = Integer.parseInt(temp[0]);
m = Integer.parseInt(temp[1]);
if (!Objects.equals(m, 30) && !Objects.equals(m, 00)) {
throw new IllegalArgumentException("请输入正确的时间范围,时间粒度为30min-" + time);
}
} catch (NumberFormatException e) {
return false;
}
if (h >= 0 && h <= 24 && m <= 60 && m >= 0) {
return true;
}
}
}
return false;
}