Android - check time between specific hours of the day

In my application, I update some things if the time is between certain hours of the day that the user selects. It works great if the user selects something like "07-21" but not from "21-07" which is overnight. As I do to check the time, I get the current hour and convert it to milliseconds. Then I check if the current milli is between the selected hours (they are also converted to milliseconds). Like this:

    if (currentMilli >= startHourMilli && currentMilli <= endHourMilli) 

      

Problem: It doesn't work if the user selects something over midnight (for example, 19-08).

I've tried a lot of things, but I just can't figure out how to do it.

Any help is appreciated!

+3


source to share


6 answers


Are you increasing the day of the year by 1 as you fly past midnight? Otherwise there startHourMilli

may be more endHourMilli

and your if clause will always be false.

The solution is to use add-method

the Calendar class. So I calculate the length of the interval in hours and add this value to our calendar instance.



int start = 21; // let take your failing example: 21-07
int end = 7;
int hours = (end - start) % 24; // here hours will be 14

Calendar cal = Calendar.getInstance();
// set calendar to TODAY 21:00:00.000
cal.set(Calendar.HOUR_OF_DAY, start);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

long startHourMilli = cal.getTimeInMillis();

// add 14 hours = TOMORROW 07:00:00.000
cal.add(Calendar.HOUR_OF_DAY, hours); 
long endHourMilli = cal.getTimeInMillis();

      

Let me know if it helps :)

+2


source


Date has functions before and after for comparing two dates. Hope this documentation helps you:

http://developer.android.com/reference/java/util/Date.html#after(java.util.Date)



http://developer.android.com/reference/java/util/Date.html#before(java.util.Date)

Sincerely.

+2


source


I am answering my own question because I think I came up with something that might work for what I am trying to do:

if(endHourMilli < startHourMilli){
if(currentMilli >= startHourMilli && currentMilli <= 23*3600000 || currentMilli >= 0 && currentMilli <= endHourMilli){
//Do whatever 
}
}else{
if (currentMilli >= startHourMilli && currentMilli <= endHourMilli) {
//Do whatever
}
}

      

It should work even if endMilli is less than startMilli, or have I screwed something up here?

0


source


I know I was a little late to the party, but I recently developed an Android app that needed to run for a given period of time, and since I didn't like working with Calendar

, I ended up using something like this:

// if start hour is later than end hour
// example: start = 21, end = 07
int startHourMilli = 21, endHourMilli = 07;
// just add one day (in your case in millis)
if (startHourMilli > endHourMilli) endHourMilli += 24 * 60 * 60 * 1000;
// now here you can check without any problems
if(currentMilli >= startHourMilli && currentMilli < endHourMilli){
    // within timeframe, do stuff you need
} else {
    // not in timeframe, find solution
}

      

Now I know you have found a solution, but I think my approach might be a little clearer (at least for beginners who might get confused).

0


source


You can always use a simple if / else for 24 hour format without using functions or additional calculations:

Example 1: Full time period from StartHour to StopHour (stop time includes all minutes)

int StartHour = 23; // start at 23:00

int StopHour = 9; // until the current hour is 9, which will include until 9:59

int CurrentHour = 2;

if (StartHour > StopHour)
{
  if (CurrentHour < StartHour && StopHour < CurrentHour) 
  {Inside = false;}
  else
  {Inside = true;}
}
else
{
  if (CurrentHour >= StartHour && StopHour >= CurrentHour) 
  {Inside = true;}
   else
  {Inside = false;}
}

      

In the end , if to true == Inside CurrentHour is in the time range StartHour - StopHour (full hour stop)

And do something else if both are equal:
  if (StartHour == StopHour) {..............};

For example2: If you want to stop at this exact second of the hour, you need some changes:

int StartHour = 23; // start at 23:00

int StopHour = 9; // this will stop after 8:59

int CurrentHour = 2;

if (StartHour2 > StopHour2)
{
  if (Hr24 < StartHour2 && StopHour2 <= Hr24) 
  {Quiet = false;}
  else
  {Quiet = true;}
}
else
{
  if (Hr24 >= StartHour2 && StopHour2 > Hr24) 
  {Quiet = true;}
   else
  {Quiet = false;}
}

      

At the end , if Inside == true CurrentHour is in the time range StartHour - StopHour (exact)

0


source


I think ottel142 is almost ok, but it would be:

 public static boolean checkIfNight() {
    int start = 21;
    int end = 7;
    int hours = 24 - start + end; 

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, start);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    long startHourMilli = cal.getTimeInMillis();
    KLog.e(cal.getTime());

    cal.add(Calendar.HOUR_OF_DAY, hours);
    long endHourMilli = cal.getTimeInMillis();

    long currentMilli = Calendar.getInstance().getTimeInMillis();


    if (currentMilli >= startHourMilli && currentMilli <= endHourMilli)
        return true;
    else return false;

}

      

0


source







All Articles