无论日期如何,确定时间是否介于两次之间

时间:2017-06-26 18:47:52

标签: java android datetime epoch

我为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

有什么方法可以比我做的更简单吗?有点困惑所有这一切。

3 个答案:

答案 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. 此方法永远不会考虑输入日期。
  2. 这只需要输入日期工厂的小时,分​​钟,秒和毫秒。
  3. 如果第一个参数是上午,第二个是下午,那么日期被视为相同。
  4. 如果第一个参数是下午,第二个参数是第二个参数,那么第二个参数是第二天。 在这种情况下,如果当前时间是晚上10点。第一个参数是晚上9点,第二个是凌晨3点,函数将返回true。第二个上午3点作为第二天的时间,晚上10点是在今天晚上9点和第二天凌晨3点之间。

答案 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)&&current.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;
      }
   }
}

(未经过测试的代码)