9

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.

Bober02
  • 15,034
  • 31
  • 92
  • 178

5 Answers5

8

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • I understand that the holidays might be skipped but this is not an issue. How do you check for a weekeday btw? I am using YearMonthDay objects – Bober02 Oct 04 '12 at 13:44
  • First of all, note that `YearMonthDay` is deprecated, you should use `LocalDate` instead. You can pass your `YearMonthDay` to the constructor of `LocalDate`. That class has a method `getDayOfWeek()` which returns the day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday). – Jesper Oct 04 '12 at 13:51
  • 2
    According to JodaTime specification 1 is Monday and 7 is Sunday. Are you sure 0 works for Sunday as well? source: http://joda-time.sourceforge.net/apidocs/org/joda/time/Chronology.html#dayOfWeek() – user1581900 Oct 04 '12 at 14:42
  • 1
    @user1581900 You're probably right. I just tried it out and got 4 for Thursday, from which I concluded that 0 must have been Sunday. (Computers always start counting from 0, right?!). – Jesper Oct 05 '12 at 07:09
  • Yeah... it gets confusing when we need to use spreadsheet and then there's no 0 row... so unintuitive :P – user1581900 Oct 05 '12 at 08:21
  • @Jesper Is incrementing the date in a loop is the only way out? Isn't there a pattern? I am trying hard to derive a mathematical function to do this, please let me know if I wont be able to derive a function for this case? – Anmol Gupta Nov 26 '15 at 16:33
5
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);
Frankie
  • 24,627
  • 10
  • 79
  • 121
vijay
  • 1,129
  • 7
  • 22
  • 34
2

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);
hendalst
  • 2,957
  • 1
  • 24
  • 25
0
    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.

Jana
  • 1
  • 1
-4

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    }
}
user1581900
  • 3,680
  • 4
  • 18
  • 21
  • Your example is incomplete, what if it's Saturday? Also, `LocalDate` is a more accurate replacement for `YearMonthDay` than `DateTime`. – Jesper Oct 04 '12 at 14:00
  • I never knew I am obliged to give COMPLETE solutions to the problems. I assumed the OP works with working days only, so it will be unlikely, that he passes Saturday or Sunday. I also assumed he is smart enough to figure out the rest on his own... On the other hand I've never dealt with JodaTime before and just like you I googled the answer... – user1581900 Oct 04 '12 at 14:37
  • 4
    Beware that this approach tries mutating the `dateTime` object, which is no possbile because `DateTime` objects are immutable. You will have to create a new instance for each iteration and return the last one, i.e. `dateTime = dateTime.plusDays(1); ... return dateTime;`. – benjamin Jan 21 '13 at 09:01