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

我看了这篇Joda time - all mondays 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/

10-12 05:44