- 1.将现有集合按照指定的长度拆分为几个子集合
- 2.两个时间进行比较
引入依赖包:
org.apache.commons commons-collections4 4.2 com.google.guava guava 23.0
public2.两个时间进行比较List > split(List
resList,int subListLength) { if (CollectionUtils.isEmpty(resList) || subListLength <= 0) { return Lists.newArrayList(); } int listLength = resList.size(); List > ansList = new ArrayList<>(); if (listLength <= subListLength) { ansList.add(resList); } else { int pre = listLength / subListLength; int last = listLength % subListLength; for (int i = 0; i < pre;i++) { ansList.add(resList.subList(subListLength * i,subListLength * (i + 1))); } if (last > 0) { ansList.add(resList.subList(subListLength * pre,resList.size())); } } return ansList; }
比较两个时间间间隔几天:
// compareIntevalDays_1("2021-12-21","2021-12-22"); 打印结果为1
public void compareIntevalDays_1(String startTime,String endTime) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate startDate = LocalDate.parse(startTime, pattern);
LocalDate endDate = LocalDate.parse(endTime, pattern);
Period period = Period.between(startDate, endDate);
System.out.println(period.getDays());
}
// compareIntevalDays_2("2021-12-21 00:00:00","2021-12-22 01:00:00");打印结果为1。
// 转化为LocalDate后,时分秒均为00:00:00,所以相差天数为1。但是在实际业务中如果两个时间只能间隔1天,此时这两个时间实际间隔1天1个小时,所以需要使用LocalDateTime
public void compareIntevalDays_2(String startTime,String endTime) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDate startDate = LocalDate.parse(startTime, pattern);
LocalDate endDate = LocalDate.parse(endTime, pattern);
Period period = Period.between(startDate, endDate);
System.out.println(period.getDays());
}
// compareIntevalDays_3("2021-12-21 00:00:00","2021-12-22 01:00:00");打印结果为1。
public void compareIntevalDays_3(String startTime,String endTime) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDateTime = LocalDateTime.parse(startTime, pattern);
LocalDateTime endDateTime = LocalDateTime.parse(endTime, pattern);
Period period = Period.between(startDateTime.toLocalDate(), endDateTime.toLocalDate()); // 这种和compareIntevalDays_2的情况一样
System.out.println(period.getDays());
Duration duration = Duration.between(startDateTime, endDateTime);
long millis = duration.toMillis();//相差毫秒数
if (millis > 24 * 60 * 60 * 1000) {
System.out.println("开始创建时间和结束创建时间差不能超过1天"); // 控制台打印此信息
}
}
比较两个时间的大小:
// compareTime("2021-12-23 00:00:00","2021-12-22 01:00:00");
public void compareTime(String startTime,String endTime) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime startDateTime = LocalDateTime.parse(startTime, pattern);
LocalDateTime endDateTime = LocalDateTime.parse(endTime, pattern);
if (startDateTime.compareTo(endDateTime) > 0) {
System.out.println("开始创建时间不能大于截止创建时间");// 控制台打印此信息
}
}



