Android - checking if time is between certain hours of the day
Asked Answered
B

7

7

in my app I'm updating some stuff if the time is between certain hours of the day which the user choose. It works fine if the user chooses something like "07-21", but not with "21-07" which is over the night. How I'm doing to check the time is I'm getting the current hour and converting it into milliseconds. Then I check if the current milli is between the chosen hours (those are also converted into milliseconds). Like this:

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

The problem: It doesn't work if the user chooses anything that is over midnight (19-08 for example).

I've tried a lot of stuff but I just can't figure out how to do this.

Any help is appreciated!

Beerbohm answered 29/1, 2013 at 21:30 Comment(2)
Can you please post the code where you set startHourMilli and endHourMilli?Freewheeling
When you say over midnight, do you mean over midday? All times except midnight are over midnight!Sandisandidge
F
4

Do you increase the day of the year by 1 when you're passing midnight? Otherwise your startHourMilli might be greater than endHourMilli and your if-clause will always be false.

The solution is to use the add-method of the Calendar class. Therefore I calculate the interval's length in hours and add this value to our Calendar instance.

int start = 21; // let's 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 this helps :)

Freewheeling answered 29/1, 2013 at 22:8 Comment(0)
F
3

Simplest way to check..

val isDayTime : Boolean
    get() {
        val cal = Calendar.getInstance()
        val hour = cal.get(Calendar.HOUR_OF_DAY)
        return hour in 8..20
    }
Frontality answered 25/8, 2021 at 11:26 Comment(0)
R
2

Date has the 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)

Best regards.

Ranice answered 29/1, 2013 at 21:38 Comment(0)
B
0

I'm answering my own question because I think I came up with something that might work for what I'm 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?

Beerbohm answered 31/1, 2013 at 15:36 Comment(0)
N
0

I know I'm a little late to the party, but recently I developed android app that needed to work within given timeframe, 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 that you found yourself a solution, but I think that my approach may be a little more understandable (at least to newbies that might get confused).

Nicaea answered 11/9, 2014 at 17:47 Comment(0)
R
0

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

For Example1: Full time period from StartHour to StopHour(stop hour include all minutes)

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

int StopHour = 9; // until current hour is 9 that 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;}
}

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

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

For Example2: If you want to stop at this exact StopHour hour, you need some changes:

int StartHour = 23; //start from 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 time range StartHour - StopHour(exact)

Redmer answered 10/10, 2014 at 14:58 Comment(0)
A
0

I think that ottel142 is almost ok but it shoud 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;

}
Anglicanism answered 26/4, 2018 at 11:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.