-1

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 can be found on my GitHub repo.

I don't think there is anything wrong with what I'm doing in that class--but maybe I am. Anyways, I then go to read the values like so:

final long nightModeStartTimeMillis = prefs.getLong("settings_night_mode_start_time",
                    Long.valueOf(getString(R.string.settings_night_mode_default_start_time)));
final long nightModeEndTimeMillis = prefs.getLong("settings_night_mode_end_time",
                    Long.valueOf(getString(R.string.settings_night_mode_default_end_time)));

My problem is that I want to use these times to determine if the current time is between them. However, I can't quite figure out how to handle the date. My defaultValue for the start time TimePreference is 1483318830000 (Sun Jan 01 20:00:30 EST 2017). I've been trying the code found from this answer, but when it comes to comparing, I think I'll never have the current time be between the default values since the date for the milliseconds never changes--only the hour and minutes.

This is my current attempt--which doesn't work

Is there some way around this that is simpler than I'm making it? Kinda confused with all of this.

Nxt3
  • 1,970
  • 4
  • 30
  • 52
  • refer to https://stackoverflow.com/questions/9816459/removing-time-from-a-date-object to find how to strip date part from timestamp. One way: use date format "HH:mm:ss.SSS" – Chenna Reddy Jun 26 '17 at 19:00
  • It's not clear to me. What's the value of `getString(R.string.settings_night_mode_default_end_time)` and `nightModeEndTimeMillis`? Can you provide some examples of inputs and expected outputs? –  Jun 26 '17 at 19:11
  • It's a timepicker. The user picks a time and I get the milliseconds of the time they selected. They never change the day's date--only the hour and minutes. `R.string.settings_night_mode_default_end_time` is just the default start time I'm suing in milliseconds, `1483318830000`. – Nxt3 Jun 26 '17 at 19:16

3 Answers3

1

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.
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • This is awesome--but only works if the times are on the same day. For example, if I want the the start time to be 9pm but the end time to be 3am--this won't work. – Nxt3 Jun 26 '17 at 21:01
  • I am taking the time is 24 hrs in the code. as you can see the HOUR_OF_DAY. so for your case you have to set calendar.(Calendar.HOUR_OF_DAY,21); calendar.(Calendar.DATE,27); for 9m. calendar.(Calendar.HOUR_OF_DAY,3); calendar.(Calendar.DATE,28); for 3am. This will give you the result. – Sagar Nayak Jun 27 '17 at 03:38
  • @Nxt3 i have modified the code. now this will take the 3am as next day if it is passed at the second param and the first is a PM. so if your time is 10pm. and the param are 9pm and 3am. this will return true. Pelase do ask if any further problem occurs, – Sagar Nayak Jun 27 '17 at 04:23
0

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.

Sarthak Gandhi
  • 2,130
  • 1
  • 16
  • 23
  • I don't think this will work if the user selects a start time of 9pm and an end time of 3am. The time will wrap around and the comparisons won't work the same. – Nxt3 Jun 26 '17 at 19:04
  • I am using the whole date object here, so it will compare the dates too. – Sarthak Gandhi Jun 26 '17 at 19:05
  • Half my problem stems from the fact that I have no clue how to make the default value for the start and end times today's date but at a specified time. So, it would have to update daily--8pm June 27th, 8pm June 28th, etc. – Nxt3 Jun 26 '17 at 19:11
  • Then you should use a date picker along with it.. because it is confusing as you cannot be sure if time lies between 2 times without days. – Sarthak Gandhi Jun 26 '17 at 19:24
  • It's used for a night mode chooser. So, if the user wants night mode to start at 9pm but end at 6am (the following day)--then this has to happen. Choosing dates makes no sense for such a feature. – Nxt3 Jun 26 '17 at 19:26
  • Then take calendar object and add hours to it. Like make a calendar object for start time then add the difference of hours to it using calendar.add(Calendar.HOURS_OF_DAY,hours) and then compare. – Sarthak Gandhi Jun 26 '17 at 19:51
0

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)

ryekos
  • 132
  • 1
  • 10