问题:
小明的合同为2020-01-01至2020-01-31,工作时间为每天的09:00至18:00。有一个兼职项目,项目周期为2020-01-01至2020-01-31,工作时间为19:00至21:00,则小明可以报名参与些兼职。如果工作时间为17:00至21:00,则因为和本职工作时间有冲突不能报名。要求写一个方法判断时间段是否有交集。需要解决跨天问题。
解决方案:
package com.yungeng.volunteer.util;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import java.time.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
/**
* @author Wesley
* @create 2020/3/23
*/
public class Util {
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class TimeBucket {
private LocalDateTime startTime;
private LocalDateTime endTime;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TimeBucket that = (TimeBucket) o;
// return !(this.startTime.after(that.endTime) || this.endTime.before(that.startTime));
// return !(this.startTime.getTime()>=that.endTime.getTime() || this.endTime.getTime()<=that.startTime.getTime());
return !(this.startTime.toEpochSecond(ZoneOffset.ofHours(8)) >= that.endTime.toEpochSecond(ZoneOffset.ofHours(8))
|| this.endTime.toEpochSecond(ZoneOffset.ofHours(8)) <= that.startTime.toEpochSecond(ZoneOffset.ofHours(8)));
}
@Override
public int hashCode() {
return Objects.hash(startTime, endTime);
}
@Override
public String toString() {
return "TimeBucket{" +
"startTime=" + startTime +
", endTime=" + endTime +
'}';
}
}
public static List<TimeBucket> getTimeBucketList(LocalDate startDate, LocalDate endDate, LocalTime startTime, LocalTime endTime) {
int days = differentDays(startDate, endDate);
List<TimeBucket> timeBucketList = new ArrayList<>();
//不跨天
if (startTime.isBefore(endTime)) {
for (int i = 0; i < days; i++) {
timeBucketList.add(new TimeBucket(LocalDateTime.of(startDate, startTime).plusDays(i), LocalDateTime.of(startDate, endTime).plusDays(i)));
}
//跨天
} else {
for (int i = 0; i < days - 1; i++) {
timeBucketList.add(new TimeBucket(LocalDateTime.of(startDate, startTime).plusDays(i), LocalDateTime.of(startDate, endTime).plusDays(1).plusDays(i)));
}
}
return timeBucketList;
}
/**
* 求两个时间点相隔天数,startDate = 2010-01-01, endDate = 2010-01-01 返回1天
*
* @param startDate
* @param endDate
* @return
*/
public static int differentDays(LocalDate startDate, LocalDate endDate) {
return (int) (endDate.toEpochDay() - startDate.toEpochDay()) + 1;
}
public static LocalDate convertToLocalDate(Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalDate();
}
public static void main(String[] args) throws Exception {
List<TimeBucket> timeBucketList = new ArrayList<>();
List<TimeBucket> myTimeBucketList = new ArrayList<>();
LocalDate startDate = LocalDate.of(2020, 3, 23);
LocalDate endDate = LocalDate.of(2020, 3, 23);
System.out.println(differentDays(startDate, endDate));
timeBucketList = getTimeBucketList(LocalDate.of(2020, 3, 1), LocalDate.of(2020, 4, 30), LocalTime.of(23, 0), LocalTime.of(2, 0));
for (TimeBucket item : timeBucketList) {
System.out.println(item);
}
myTimeBucketList = getTimeBucketList(LocalDate.of(2020, 3, 1), LocalDate.of(2020, 3, 31), LocalTime.of(1, 0), LocalTime.of(3, 0));
***System.out.println(CollectionUtils.containsAny(timeBucketList,myTimeBucketList));***
}
}