0

I need to figure out a way to get one day prior date not including weekends.

Such as: Suppose its July 1, 2013 and i want to get one day prior date so it should return June 28, 2013 not June 30, 2013.

I have used Calendar library a lot but after researching while it looks like JodaTime library is way to go.

4

1 回答 1

4

这样的事情应该这样做:

static LocalDate oneDayBackExcludingWeekends(LocalDate date) {
    do {
        date = date.minusDays(1);
    } while (date.getDayOfWeek() == DateTimeConstants.SUNDAY ||
             date.getDayOfWeek() == DateTimeConstants.SATURDAY); 
    return date;
}

请注意,您可能会更有效率:

static LocalDate oneDayBackExcludingWeekends(LocalDate date) {
    switch (date.getDayOfWeek()) {
        case DateTimeConstants.MONDAY:
            return date.minusDays(3);
        case DateTimeConstants.SUNDAY:
            return date.minusDays(2);
        default:
            return date.minusDays(1);
    }
}
于 2013-07-02T19:39:44.807 回答