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