1. Overview
1.概述
In this tutorial, we will briefly look at the algorithm to skip weekends while adding days to a LocalDate instance in Java 8.
在本教程中,我们将简要介绍在Java 8中向LocalDate 实例添加日子时跳过周末的算法。
We’ll also go through the algorithm to subtract days from LocalDate object whilst skipping weekends.
我们还将通过算法来从LocalDate对象中减去日子,同时跳过周末。
2. Adding Days
2.增加天数
In this method, we keep on adding one day to the LocalDate object until we have added the required numbers of days. While adding a day, we check whether the day of the new LocalDate instance is a Saturday or a Sunday.
在这个方法中,我们不断向LocalDate对象添加一天,直到我们添加了所需的天数。在添加一天时,我们检查新的LocalDate实例的日期是星期六还是星期日。
If the check returns true, then we don’t increment the counter for the number of days added until that point. However, if the current day is a weekday, then we increment the counter.
如果检查的结果是true,那么在这之前,我们不会递增增加的天数的计数器。然而,如果当前的日子是一个工作日,那么我们就增加计数器。
In this way, we keep on adding days until the counter is equal to the number of days that are supposed to be added:
通过这种方式,我们不断地增加天数,直到计数器等于应该增加的天数。
public static LocalDate addDaysSkippingWeekends(LocalDate date, int days) {
LocalDate result = date;
int addedDays = 0;
while (addedDays < days) {
result = result.plusDays(1);
if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY || result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
++addedDays;
}
}
return result;
}
In the above code, we use the plusDays() method of LocalDate object to add days to the result object. We increment the addedDays variable only when the day is a weekday. When the variable addedDays is equal to days variable, we stop adding a day to result LocalDate object.
在上面的代码中,我们使用LocalDate对象的plusDays()方法来为result对象添加天数。只有当这一天是工作日时,我们才会增加addedDays变量。当变量addedDays等于days变量时,我们停止向result LocalDate对象添加日子。
3. Subtracting Days
3.减去天数
Similarly, we can subtract days from LocalDate object using the minusDays() method until we have subtracted the required number of days.
同样,我们可以使用minusDays()方法从LocalDate对象中减去天数,直到我们减去所需的天数。
To achieve this, we’ll keep a counter for the number of days subtracted that is incremented only when the resulted day is a weekday:
为了实现这一点,我们将为被减去的天数保留一个计数器,只有当结果日是工作日时才会递增。
public static LocalDate subtractDaysSkippingWeekends(LocalDate date, int days) {
LocalDate result = date;
int subtractedDays = 0;
while (subtractedDays < days) {
result = result.minusDays(1);
if (!(result.getDayOfWeek() == DayOfWeek.SATURDAY || result.getDayOfWeek() == DayOfWeek.SUNDAY)) {
++subtractedDays;
}
}
return result;
}
From the above implementation, we can see that subtractedDays is only incremented when the result LocalDate object is a weekday. Using the while loop, we subtract days until subtractedDays is equal to the days variable.
从上面的实现中,我们可以看到subtractedDays只有在result LocalDate对象是一个工作日时才会被递增。使用while循环,我们减去日子,直到subtractedDays等于days变量。
4. Conclusion
4.总结
In this brief article, we looked at algorithms for adding days to and subtracting days from LocalDate object skipping weekends. Furthermore, we looked at their implementations in Java.
在这篇简短的文章中,我们看了向LocalDate对象添加天数和减去天数的算法跳过周末。此外,我们还研究了它们在Java中的实现。
As always, the full source code of the working examples is available over on GitHub.
一如既往,工作实例的完整源代码可在GitHub上获得over。