判断多个时间是否排序
思路当多参的时间传过来,先排序,然后遍历集合对比是否连续的只要一处不是连续的就直接返回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可以自动排序文章来源:https://www.toymoban.com/news/detail-713637.html
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);
}
返回结果文章来源地址https://www.toymoban.com/news/detail-713637.html
true
Process finished with exit code 0
到了这里,关于判断时间是否连续的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!