Time: How to get the next friday ?

Time: How to get the next friday ?

Asked on December 28, 2018 in Java.
Add Comment


  • 5 Answer(s)

    java.time
    In the java 8 and later versions we have framework called java.time. Here ,in order to get the next or previous day-of-week we can use  TemporalAdjusters.

    private LocalDate calcNextFriday(LocalDate d) {
      return d.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
    }
    
    Answered on December 28, 2018.
    Add Comment

    Try to use this code,

    private LocalDate calcNextFriday3(LocalDate d) {
        return d.isBefore(d.dayOfWeek().setCopy(5))?d.dayOfWeek().setCopy(5):d.plusWeeks(1).dayOfWeek().setCopy(5);
    }
    

    Otherwise use this one,

    private LocalDate calcNextDay(LocalDate d, int weekday) {
        return (d.getDayOfWeek() < weekday)?d.withDayOfWeek(weekday):d.plusWeeks(1).withDayOfWeek(weekday);
    }
     
    private LocalDate calcNextFriday2(LocalDate d) {
        return calcNextDay(d,DateTimeConstants.FRIDAY);
    }
    
    Answered on December 28, 2018.
    Add Comment
    if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
        return d.withDayOfWeek(DateTimeConstants.FRIDAY));
    } else if (d.getDayOfWeek() == DateTimeConstants.FRIDAY) {
        // almost useless branch, could be merged with the one above
        return d;
    } else {
        return d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
    }
    

    another simple kind of approach,

    private LocalDate calcNextFriday(LocalDate d) {
        if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
            d = d.withDayOfWeek(DateTimeConstants.FRIDAY));
        } else {
            d = d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
        }
        return d; // note that there's a possibility original object is returned
    }
    

    More short form,

    private LocalDate calcNextFriday(LocalDate d) {
        if (d.getDayOfWeek() >= DateTimeConstants.FRIDAY) {
            d = d.plusWeeks(1);
        }
        return d.withDayOfWeek(DateTimeConstants.FRIDAY);
    }
    
    Answered on December 28, 2018.
    Add Comment

    It’s possible to do it in a much easier to read way:

    if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
        return d.withDayOfWeek(DateTimeConstants.FRIDAY));
    } else if (d.getDayOfWeek() == DateTimeConstants.FRIDAY) {
        // almost useless branch, could be merged with the one above
        return d;
    } else {
        return d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
    }

    or in a bit shorter form

    private LocalDate calcNextFriday(LocalDate d) {    
        if (d.getDayOfWeek() < DateTimeConstants.FRIDAY) {
            d = d.withDayOfWeek(DateTimeConstants.FRIDAY));
        } else {
            d = d.plusWeeks(1).withDayOfWeek(DateTimeConstants.FRIDAY));
        }    
        return d; // note that there's a possibility original object is returned
    }

    or even shorter

    private LocalDate calcNextFriday(LocalDate d) {
        if (d.getDayOfWeek() >= DateTimeConstants.FRIDAY) {
            d = d.plusWeeks(1);
        }
        return d.withDayOfWeek(DateTimeConstants.FRIDAY);
    }
    Answered on January 16, 2019.
    Add Comment
    private LocalDate calcNextFriday3(LocalDate d) {
        return d.isBefore(d.dayOfWeek().setCopy(5))?d.dayOfWeek().setCopy(5):d.plusWeeks(1).dayOfWeek().setCopy(5);
    }

    Alternative approach

    private LocalDate calcNextDay(LocalDate d, int weekday) {
        return (d.getDayOfWeek() < weekday)?d.withDayOfWeek(weekday):d.plusWeeks(1).withDayOfWeek(weekday);
    }
    
    
    private LocalDate calcNextFriday2(LocalDate d) {
        return calcNextDay(d,DateTimeConstants.FRIDAY);
    }
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.