问题描述
我为Android Wear表盘创建了自定义 TimePicker
首选项。用户选择一个时间并以毫秒为单位返回当前时间。可以在我的。
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)));
我的问题是我想用这些时间来确定当前时间是否在它们之间。但是,我无法弄清楚如何处理日期。我的 defaultValue
开头时间 TimePreference
是 1483318830000
(Sun 1月01日20:00:30 EST 2017)。 ,但在比较时,我想我永远不会有当前时间介于默认值之间,因为毫秒的日期永远不会改变 - 只有小时和分钟。
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.
有什么方法可以比我做的更简单吗?有点困惑所有这一切。
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 如果当前时间介于两次之间
TRUE if the current time is in between two times
FALSE 如果当前时间不在两次之间
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
- 此方法永远不会考虑输入日期。
- 这只需要一小时,输入日期工厂的分钟,秒和毫秒。
- 如果第一个参数是am,第二个是pm,则日期被视为相同。
- 如果第一个参数是下午,第二个参数是第二个参数,那么第二个参数是第二天。
在这种情况下,如果当前时间是晚上10点。第一个参数是晚上9点,第二个是凌晨3点,函数将返回true。第二个上午3点作为第二天的时间,晚上10点是在今天和第二天凌晨3点之间的晚上9点。
- 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.
这篇关于无论日期如何,确定时间是否介于两次之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!