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

落花浮王杯 提交于 2019-12-22 09:32:35

问题


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!


回答1:


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




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




回答3:


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?




回答4:


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




回答5:


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)




回答6:


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;

}


来源:https://stackoverflow.com/questions/14592669/android-checking-if-time-is-between-certain-hours-of-the-day

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!