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.
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
- This method will never take the input date into regard.
- This will only take the hour, minute, seconds and milliseconds from the input date mills.
- if the first param is a am and the second is a pm then the date is considered as same.
- 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)&¤t.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)
来源:https://stackoverflow.com/questions/44766652/determine-if-a-time-is-between-two-times-regardless-of-date