2

In my application user can chose date with Calendar and then chose how many days he wants add.

Adding days working excellent, but I need to skip weekdays.

For example you will choose day and add +15 days, result will be Saturday or Sunday but in this case I need to get always Monday if result is one of these days.

Here is method for adding days

public static Date addDays(Date date, int days) {
     Calendar cal = Calendar.getInstance();
     cal.setTime(date);
     cal.add(Calendar.DATE, days + FirstClass.hotovo); 
     return cal.getTime();
}

Thank you for your help, I am not expert in programming. I am amateur and i am still learning..

JRG
  • 4,037
  • 3
  • 23
  • 34
Daniel Vágner
  • 538
  • 3
  • 19
  • 1
    Why don't you just check if the resulting day is Saturday or Sunday and add 2 or 1 additional day/s accordingly? – Juan Jul 25 '17 at 21:17

2 Answers2

2

You should check what is the resultant day i.e. check if its SATURDAY or SUNDAY and then adding 2 or 1 to get next MONDAY.

NOTE: I do not know what is FirstClass.hotovo so I have removed temporary from below code and you can add it as you would have it in your project. Below is to demonstrate how to check day and add 1 or 2 day respectively.

Here is the sample code.

Caller:

addDays(new Date(), 18);

Your method:

public static Date addDays(Date date, int days) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, days);
    Log.d("TEST", "BEFORE CHECKING: " + cal.getTime().toString());
    // SATURDAY is the last day of week so add 2 days
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
        cal.add(Calendar.DATE, 2);
        // SUNDAY is the first day of week so add 1 day
    } else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        cal.add(Calendar.DATE, 1);
    } // else not required as it means its one of the week day
    Log.d("TEST", "AFTER UPDATING: " + cal.getTime().toString());
    return cal.getTime();
}

Sample Run

Resultant day was SATURDAY so adding 2 days to get MONDAY

07-25 15:46:55.729 4219-4219/? D/TEST: BEFORE CHECKING: Sat Aug 12 15:46:55 PDT 2017
07-25 15:46:55.729 4219-4219/? D/TEST: AFTER UPDATING: Mon Aug 14 15:46:55 PDT 2017

Resultant day was SUNDAY so adding 1 day to get MONDAY

07-25 15:47:57.634 4322-4322/? D/TEST: BEFORE CHECKING: Sun Aug 13 15:47:57 PDT 2017
07-25 15:47:57.634 4322-4322/? D/TEST: AFTER UPDATING: Mon Aug 14 15:47:57 PDT 2017

Resultant day was TUESDAY so not adding any more days

07-25 15:52:27.115 4445-4445/? D/TEST: BEFORE CHECKING: Tue Aug 15 15:52:27 PDT 2017
07-25 15:52:27.115 4445-4445/? D/TEST: AFTER UPDATING: Tue Aug 15 15:52:27 PDT 2017
JRG
  • 4,037
  • 3
  • 23
  • 34
  • Thank you wery much, this is exactly what i need. But i have 1 more question. How can i skip like this holiday. I need to create a ArayList - add every day with holiday or in android studio is something for that ? – Daniel Vágner Jul 30 '17 at 11:49
  • Create an ArrayList of Date with holidays and compare them with the resultant day to check if it's holiday and also either create a map of Day and no. of days to be added for next week day or write logic with switch case to add days once you identify its holiday and what day is next working day. – JRG Jul 30 '17 at 15:49
2

For an operation like this I definitely recommend using the modern Java date and time API. Not only is it generally much nicer to work with than the outdated classes Date and Calendar. It also has some specific advantages in your situation: (1) It offers a LocalDate that represents a date without time of day, which seems to model your data more precisely than the outdated Date, which always includes time-of-day too. (2) Adding days and checking day-of-week is more convenient.

To use the modern API on Android you will need to get the ThreeTenABP, see this question: How to use ThreeTenABP in Android Project. To use it in Java 8 or later, just dig in, it’s built-in.

Your method becomes

public static LocalDate addDays(LocalDate date, int days) {
    date = date.plusDays(days + FirstClass.hotovo);
    // weekend?
    DayOfWeek dow = date.getDayOfWeek();
    if (dow.equals(DayOfWeek.SATURDAY) || dow.equals(DayOfWeek.SUNDAY)) {
        date = date.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
    }
    return date;
}

Please note how clear and self-explanatory the code is compared to code using the old classes (if you think the part with the temporal adjuster looks a bit tricky, you may instead just use date.plusDays() again; I wanted to demonstrate the temporal adjuster to give a bit of impression of the power of the newer classes).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161