Determine if a time is between two times regardless of date

前端 未结 3 1910
死守一世寂寞
死守一世寂寞 2021-01-29 10:06

I created a custom TimePicker preference for my Android Wear watch face. The user selects a time and it returns the current time in milliseconds. The code for this

相关标签:
3条回答
  • 2021-01-29 10:38

    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.
    0 讨论(0)
  • 2021-01-29 10:51

    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.

    0 讨论(0)
  • 2021-01-29 10:51

    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)

    0 讨论(0)
提交回复
热议问题