gpt4 book ai didi

java - 给定两个不包括周末的日期计算日期

转载 作者:搜寻专家 更新时间:2023-11-01 01:06:54 24 4
gpt4 key购买 nike

我在 Spring 3.0 项目中使用 Joda time api 来计算日期。现在我有一个开始和结束日期,我想在这两个日期之间得到除周末或周六或周日之外的每一天。我怎样才能做到这一点?

我看了这篇文章Joda time - all mondays between two dates .它提供了一些指导,但对于如何排除两个日期仍然含糊不清。

最佳答案

我假设你的问题是如何

get every day except weekend or Saturday or Sunday between two dates.

解决方案:

public static void main(String[] args) {
final LocalDate start = LocalDate.now();
final LocalDate end = new LocalDate(2012, 1, 14);

LocalDate weekday = start;

if (start.getDayOfWeek() == DateTimeConstants.SATURDAY ||
start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
}

while (weekday.isBefore(end)) {
System.out.println(weekday);

if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
weekday = weekday.plusDays(3);
else
weekday = weekday.plusDays(1);
}
}

关于java - 给定两个不包括周末的日期计算日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8171968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com