Determine if a time is between two times regardless of date

时光毁灭记忆、已成空白 提交于 2019-12-02 13:05:52

Provide two times to this function is long and you get

TRUE if the current time is in between two times

FALSE if the current time is not in between two times provided

boolean checkIfCurrentTimeInBetweenRegardlessOfDate(long timeOne, long timeTwo) {

    Calendar calendarOne = Calendar.getInstance();
    calendarOne.setTimeInMillis(timeOne);
    Calendar calendarTwo = Calendar.getInstance();
    calendarTwo.setTimeInMillis(timeTwo);

    Calendar calendarCurrentTime = Calendar.getInstance();
    calendarCurrentTime.set(Calendar.HOUR_OF_DAY, 22);

    Calendar calendarOneToCompare = Calendar.getInstance();
    calendarOneToCompare.setTimeInMillis(calendarCurrentTime.getTimeInMillis());
    calendarOneToCompare.set(Calendar.HOUR_OF_DAY, calendarOne.get(Calendar.HOUR_OF_DAY));
    calendarOneToCompare.set(Calendar.MINUTE, calendarOne.get(Calendar.MINUTE));
    calendarOneToCompare.set(Calendar.SECOND, calendarOne.get(Calendar.SECOND));
    calendarOneToCompare.set(Calendar.MILLISECOND, calendarOne.get(Calendar.MILLISECOND));

    Calendar calendarTwoToCompare = Calendar.getInstance();
    calendarTwoToCompare.setTimeInMillis(calendarCurrentTime.getTimeInMillis());
    calendarTwoToCompare.set(Calendar.HOUR_OF_DAY, calendarTwo.get(Calendar.HOUR_OF_DAY));
    calendarTwoToCompare.set(Calendar.MINUTE, calendarTwo.get(Calendar.MINUTE));
    calendarTwoToCompare.set(Calendar.SECOND, calendarTwo.get(Calendar.SECOND));
    calendarTwoToCompare.set(Calendar.MILLISECOND, calendarTwo.get(Calendar.MILLISECOND));

    int AM_CALENDAR = Calendar.AM;
    int PM_CALENDAR = Calendar.PM;

    if (Integer.parseInt(String.valueOf(calendarOne.get(Calendar.AM_PM))) == PM_CALENDAR
            && Integer.parseInt(String.valueOf(calendarTwo.get(Calendar.AM_PM))) == AM_CALENDAR) {
        calendarTwoToCompare.add(Calendar.DATE, 1);
    }

    return (calendarOneToCompare.compareTo(calendarCurrentTime) < 0
            && calendarCurrentTime.compareTo(calendarTwoToCompare) < 0);
}

Instructions

  1. This method will never take the input date into regard.
  2. This will only take the hour, minute, seconds and milliseconds from the input date mills.
  3. if the first param is a am and the second is a pm then the date is considered as same.
  4. if the first param is pm and the second is am the second param is taken as a next day. let in this case if the current time is 10pm. the first params is 9pm and the second is 3am the function will return a true. the second param 3am is taken as time of next day and 10pm is in between 9pm if today and 3am of next day.

What you can do is create a 2 date objects from this time using this:

Date first = new Date();
first.setTime(time_in_milliseconds);

Date second = new Date();
first.setTime(time_in_milliseconds);

Then you can get current time by this

Date current = new Date();

Then use this condition:

if(current.after(first)&&current.before(second)){

//Do your work

}

Hope this helps.

Try implementing something like this method below. Might need some modifications.

private boolean isTimeBetween(long startMillis, long endMillis, long testMillis) {
   long startHour = (startMillis / (1000 * 60 * 60)) % 24;
   long startMinute = (startMillis / (1000 * 60)) % 60;
   long startSecond = (startMillis / 1000) % 60;
   long endHour = (endMillis / (1000 * 60 * 60)) % 24;
   long endMinute = (endMillis / (1000 * 60)) % 60;
   long endSecond = (endMillis / 1000) % 60;
   long testHour = (testMillis / (1000 * 60 * 60)) % 24;
   long testMinute = (testMillis / (1000 * 60)) % 60;
   long testSecond = (testMillis / 1000) % 60;

   if (startHour < endHour) {
      if (testHour > startHour && testHour < endHour) {
         return true;
      } else if ((testHour == startHour && testMinute > startMinute) || (testHour == endHour && testMinute < endMinute)) {
         return true;
      } else if ((testHour == startHour && testMinute == startMinute && testSecond > startSecond) || (testHour == endHour && testMinute == endMinute && testSecond < endSecond)) {
         return true;
      } else {
         return false;
      }
   } else if (startHour > endHour) {
      if ((testHour > startHour && testHour <= 24) && (testHour < endHour && testHour >= 0)) {
         return true;
      } else if ((testHour == startHour && testMinute > startMinute || (testHour == endHour && testMinute < endMinute))) {
         return true;
      } else if ((testHour == startHour && testMinute == startMinute && testSecond > startSecond || (testHour == endHour && testMinute == endMinute && testSecond < endSecond))) {
         return true;
      } else {
         return false;
      }
   } else {
      if (testMinute > startMinute && testMinute < endMinute) {
         return true;
      } else if ((testMinute == startMinute && testSecond > startSecond) || (testMinute == endMinute && testSecond < endSecond)) {
         return true;
      } else {
         return false;
      }
   }
}

(Code not tested)

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