示例
到期日为15分钟
调度
星期一至五:7:30-17:00
星期六上午九时至下午二时
太阳上午10:00 -下午1:00
如果我的初次约会( IniDate )是周一6:00,到期日是周一7:45,如果我的IniDate是周一8:45,我的预产期将是周一8:15,但是如果我的IniDate是周一16:50,我的预产期将是周二的7:35,因为我从周一开始有10分钟,第二天有5分钟,如果我的IniDate是周一18:00,我的预产期将是星期二7:45,在Talend中工作,这将使用实际的日期,也就是说,假设我的IniDate是05/05/2020 18:00,那么我的到期日将是06/05/2020 7:45,考虑到时间表,这是我的第一个问题,试图在中间变量部分的tMap组件中使用它。
发布于 2020-05-06 16:58:31
将适当的时间表示为Duration
Duration dueTime = Duration.ofMinutes(15);将您的计划表示为EnumMap<DayOfWeek, DailySchedule>,其中DailySchedule是您为每日打开和关闭时间编写的类。在类中,将时间表示为LocalTime对象。类可能有一些方法来确定一天中给定的时间是在调度间隔之前、之内还是之后。
最好将您最初的时间为05/05/2020 18:00作为相关时区的ZonedDateTime (LocalDateTime也可能工作,但确实不正确)。
考虑到你最初的日期和时间,把一周中的一天从它中拿出来。在地图上查找每日的日程安排。如果时间不在计划间隔内,则首先将其调整为当前或明天的间隔开始。
第一次拍摄在你的截止日期和时间是调整的日期和时间加上适当的时间。ZonedDateTime有一个接受Duration的plus方法。现在,如果到期日是下一个日期,或者是今天的关闭时间之后,这是不正确的。在本例中,使用Duration.between计算一天所需的时间(示例中的10分钟)。从适当的时间(Duration.minus())中减去这一点。现在从第二天的开放时间开始。检查实际上应该在一个循环中进行,以考虑到每天的日程安排可能比应有的时间短。如果星期六的时间表是09:00-09:05和周日10:00-10:05,我们可能必须从星期五到星期一才能找到正确的到期日和时间。
Duration、DayOfWeek、LocalTime和ZonedDateTime都属于java.time (现代Java和time )。教程链接在底部。
稍后编辑:代码
我可以这样做:
Map<DayOfWeek, DailySchedule> weeklySchedule = new EnumMap<>(DayOfWeek.class);
DailySchedule weekdaySchedule
= new DailySchedule(LocalTime.of(7, 30), LocalTime.of(17, 0));
for (DayOfWeek dow = DayOfWeek.MONDAY;
dow.getValue() <= DayOfWeek.FRIDAY.getValue(); dow = dow.plus(1)) {
weeklySchedule.put(dow, weekdaySchedule);
}
weeklySchedule.put(DayOfWeek.SATURDAY,
new DailySchedule(LocalTime.of(9, 0), LocalTime.of(14, 0)));
weeklySchedule.put(DayOfWeek.SUNDAY,
new DailySchedule(LocalTime.of(10, 0), LocalTime.of(13, 0)));
Duration dueTime = Duration.ofMinutes(15);
// Set initial day and time
DayOfWeek currentDay = DayOfWeek.MONDAY;
LocalTime currentTime = LocalTime.of(16, 50);
Duration remainingTimeToAdd = dueTime;
DailySchedule todaysSchedule = weeklySchedule.get(currentDay);
if (todaysSchedule.isBeforeOpen(currentTime)) {
currentTime = todaysSchedule.getOpen();
} else if (todaysSchedule.isOnOrAfterClose(currentTime)) {
currentDay = currentDay.plus(1);
todaysSchedule = weeklySchedule.get(currentDay);
currentTime = todaysSchedule.getOpen();
}
// We will break this loop explicitly when done
while (true) {
// Can time be added today?
LocalTime candidateDueTime = currentTime.plus(remainingTimeToAdd);
if (todaysSchedule.isWithinSchedule(candidateDueTime)) {
// yes, done
currentTime = candidateDueTime;
break;
} else {
// take remainder of today and continue tomorrow
remainingTimeToAdd = remainingTimeToAdd.minus(Duration.between(currentTime, todaysSchedule.getClose()));
currentDay = currentDay.plus(1);
todaysSchedule = weeklySchedule.get(currentDay);
currentTime = todaysSchedule.getOpen();
}
}
System.out.println("Due day and time: " + currentDay + " at " + currentTime);该示例的输出:
到期日及时间:星期二07:35
如果适当的时间足够长,从一天的关闭到第二天的打开,代码将无法工作。并且缺少各种验证和检查,您会想要添加它们。
链接
Oracle教程:日期时间解释了如何使用java.time。
https://stackoverflow.com/questions/61623362
复制相似问题