Eenvincible
Eenvincible

Reputation: 5626

Handle Days And Hours Before Today & Now In Time Picker

In my android app, I have a simple UI with two elements(widgets) : a spinner that lets users pick a DAY_OF_WEEK & a TimePicker that let's them choose time of day and minutes (AM/PM) included.

Here is the UI:

Spinner and time picker

There are three possible outcomes in an event that a user updates their settings:

  1. The user picks a day that is ahead of Today (for example if Today is Wednesday, they pick Thursday or Friday etc). In this case, everything should be totally fine.
  2. The user picks a day that is before today (for example if Today is Wednesday, they pick Tuesday or Monday etc). In this case, I need to figure out how to set the alarm for the selected day BUT next week!
  3. The user picks Today but a time that is before Now (for example if Today is Wednesday, they pick Today and select a time like 4AM (it is 6:54 AM right now). In this case, I need to set the alarm for the selected day (which is the same day as today) but since time has passed, I have to make it Next week).

I do not know why I feel confused here but here is the code that I have tried to write to handle the situation:

    int hour = Utils.getValueFromSharedPrefs(context.getString(R.string.hour_key), activity);
    int minutes = Utils.getValueFromSharedPrefs(context.getString(R.string.minute_key), activity);

    if (day >= Calendar.DAY_OF_WEEK){
        if (hour >= Calendar.HOUR_OF_DAY){
            calendar.set(Calendar.DAY_OF_WEEK, day);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minutes);
            calendar.set(Calendar.SECOND, 0);
        }else{
            int diff = Calendar.DAY_OF_WEEK - day;
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK + diff);
            calendar.set(Calendar.HOUR_OF_DAY, hour);
            calendar.set(Calendar.MINUTE, minutes);
            calendar.set(Calendar.SECOND, 0);
        }
    }else{
        calendar.set(Calendar.DAY_OF_WEEK, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minutes);
        calendar.set(Calendar.SECOND, 0);
    }

The variable day is an integer equivalent of the day selected by a user from the spinner.

My question is this: what is the best approach to solving this problem? I just want to make sure that when a user selects Today[WEDNESDAY] and a time less than right now, I set the calendar for next week since that hour has already passed. The same should be true for when a user selects a day before today [e.g TUESDAY, MONDAY, SUNDAY] - the alarm should be set for Next week on the selected day.

Thank you in advance.

Upvotes: 1

Views: 327

Answers (1)

Aman Aalam
Aman Aalam

Reputation: 11251

It's always a good idea, according to me, to convert all your Calendar values to a timestamp, do the math on it, and then convert them back.

Makes it easier for you.

Upvotes: 1

Related Questions