我为Android Wear表盘创建了自定义TimePicker
首选项。用户选择时间并以毫秒为单位返回当前时间。可以在我的GitHub repo上找到此代码。
我认为我在课堂上所做的事情没有任何问题 - 但也许我是。无论如何,我接着去阅读这样的值:
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)));
我的问题是我想用这些时间来确定当前时间是否在它们之间。但是,我无法弄清楚如何处理日期。开始时间defaultValue
的{{1}}为TimePreference
(2017年1月1日20:00:30,2017年)。 I've been trying the code found from this answer,但是在比较时,我想我永远不会将当前时间介于默认值之间,因为毫秒的日期永远不会改变 - 只有小时和分钟。
This is my current attempt--which doesn't work
有什么方法可以比我做的更简单吗?有点困惑所有这一切。
答案 0 :(得分:1)
提供两次此功能很长,你得到
如果当前时间介于两次之间TRUE
FALSE 如果当前时间不在两次之间
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);
}
<强> 说明 强>
答案 1 :(得分:0)
您可以使用以下方法创建2个日期对象:
Date first = new Date();
first.setTime(time_in_milliseconds);
Date second = new Date();
first.setTime(time_in_milliseconds);
然后你可以通过这个
获得当前时间Date current = new Date();
然后使用这个条件:
if(current.after(first)&¤t.before(second)){
//Do your work
}
希望这有帮助。
答案 2 :(得分:0)
尝试在下面实现类似此方法的内容。可能需要一些修改。
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;
}
}
}
(未经过测试的代码)