判断多个时间是否排序
思路当多参的时间传过来,先排序,然后遍历集合对比是否连续的只要一处不是连续的就直接返回false
//判断时间是否连续的
public static Boolean verificationDate(Date... dates) throws ParseException {
boolean flag = true;
SortedSet<Date> setDate = new TreeSet<>();
//入参排序
for (Date date : dates) {
String dateStr = sdf.format(date);
setDate.add(sdf.parse(dateStr));
}
int count = 1;
for (Date date : setDate) {
if (count < setDate.size()) {
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, 1);
Date newDate = calendar.getTime();
Date parse = sdf.parse(sdf.format(newDate));
if (!setDate.contains(parse)) {
flag = false;
break;
}
count++;
}
}
return flag;
}
使用TreeSet可以自动排序
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
public static Calendar calendar = Calendar.getInstance();
public static void main(String[] args) throws ParseException {
Boolean aBoolean = verificationDate(sdf.parse("2023-05-12"),
sdf.parse("2023-05-10"),
sdf.parse("2023-05-11"));
System.out.println(aBoolean);
}
返回结果
true
Process finished with exit code 0