简体   繁体   中英

Add days, android studio

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..

You should check what is the resultant day ie 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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM