joda time - add weekdays to date

前端 未结 5 1558
情书的邮戳
情书的邮戳 2020-11-30 15:11

Is it possible to add weekdays to joda time?

For instance, if current date is Friday 01/03, date + 1 should return Monday 04/03, rather than 02/03.

相关标签:
5条回答
  • 2020-11-30 15:31
    LocalDate newDate = new LocalDate();
    int i=0;
    while(i<days)//days == as many days as u want too
    {
        newDate = newDate.plusDays(1);//here even sat and sun are added
        //but at the end it goes to the correct week day.
        //because i is only increased if it is week day
        if(newDate.getDayOfWeek()<=5)
        {
            i++;
        }
    
    }
    System.out.println("new date"+newDate);
    
    0 讨论(0)
  • 2020-11-30 15:38

    As far as I know there is no built-in method to automatically do this for you in Joda Time. However, you could write your own method, that increments the date in a loop until you get to a weekday.

    Note that, depending on what you need it for exactly, this could be (much) more complicated than you think. For example, should it skip holidays too? Which days are holidays depends on which country you're in. Also, in some countries (for example, Arabic countries) the weekend is on Thursday and Friday, not Saturday and Sunday.

    0 讨论(0)
  • 2020-11-30 15:38

    Be aware that iterating through adding N days one at a time can be relatively expensive. For small values of N and/or non performance sensitive code, this is probably not an issue. Where it is, I'd recommend minimizing the add operations by working out how many weeks and days you need to adjust by:

    /**
     * Returns the date that is {@code n} weekdays after the specified date.
     * <p>
     * Weekdays are Monday through Friday.
     * <p>
     * If {@code date} is a weekend, 1 weekday after is Monday.
     */
    public static LocalDate weekdaysAfter(int n, LocalDate date) {
        if (n == 0)
            return date;
        if (n < 0)
            return weekdaysBefore(-n, date);
        LocalDate newDate = date;
        int dow = date.getDayOfWeek();
        if (dow >= DateTimeConstants.SATURDAY) {
            newDate = date.plusDays(8 - dow);
            n--;
        }
        int nWeeks = n / 5;
        int nDays = n % 5;
        newDate = newDate.plusWeeks(nWeeks);
        return ( (newDate.getDayOfWeek() + nDays) > DateTimeConstants.FRIDAY)
                ? newDate.plusDays(nDays + 2)
                : newDate.plusDays(nDays);
    
    0 讨论(0)
  • 2020-11-30 15:43
        public LocalDate getBusinessDaysAddedDate(LocalDate localDate, int businessDays){
    
            LocalDate result;
            if(localDate.getDayOfWeek().getValue() + businessDays > 5) {
                result = localDate.plusDays(2);
            }
            result = localDate.plusDays(businessDays);
    
            return result;
        }
    

    In order to work with Date instead of LocalDate, refer https://stackoverflow.com/a/47719540/12794444 for the conversions.

    0 讨论(0)
  • 2020-11-30 15:46

    Class YearMonthDay is deprecated and you shouldn't use it. If you change to simple DateTime you can obtain the week day by calling:

    dateTime.getDayOfWeek();
    

    For Friday it will be 5.

    One of the approaches can be making a custom addDays method which should look something like that:

    addDays(DateTime dateTime, int days) {
        for(int i=0;i<days;i++){
            dateTime.plusDays(1);
            if(dateTime.getDayOfWeek()==6) dateTime.plusDays(2); // if Saturday add 2 more days    }
    }
    
    0 讨论(0)
提交回复
热议问题