2017-03-02 94 views
4

我想比較2天(實際上是3)。在我的使用案例中,我需要比較3個日期。一個是StartSleepingTime,StopSleepingTime和Now。我的應用程序監視用戶的活動級別,並且我有一個每半小時運行一次的計劃任務任務,以檢查NOW是否在用戶的StartSleepingTime和StopSleepingTime之間,以確保在此間隔內停止活動監控服務。由於用戶在運行計劃任務時第一次登錄時設置了StartSleepingTime和StopSleepingTime,與NOW相比,它們都處於「過去」狀態。目前,我試圖從3個日期只提取小時和分鐘的信息,做這樣的比較:僅比較Java中的小時和分鐘的2個日期?

public static boolean compareHrsAndMintsOnly(Date startSH, Date now, Date stopSH) { 
    boolean isNowWithinSleepingTime = false; 

    Calendar startSHCalendar = Calendar.getInstance(); 
    startSHCalendar.setTime(startSH); 
    Calendar nowCalendar = Calendar.getInstance(); 
    nowCalendar.setTime(now); 
    Calendar stopSHCalendar = Calendar.getInstance(); 
    stopSHCalendar.setTime(stopSH); 

    int startSHhour = startSHCalendar.get(Calendar.HOUR_OF_DAY); 
    int startSHmin = startSHCalendar.get(Calendar.MINUTE); 

    int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY); 
    int nowMin = nowCalendar.get(Calendar.MINUTE); 

    int stopSHhour = stopSHCalendar.get(Calendar.HOUR_OF_DAY); 
    int stopSHmin = stopSHCalendar.get(Calendar.MINUTE); 

    if ((startSHhour > nowHour && startSHmin > nowMin) && (nowHour < stopSHhour && nowMin < stopSHmin)) { 
     // stop the monitoring service 
     isNowWithinSleepingTime = true; 
    }else { 
     // start the monitoring service 

     isNowWithinSleepingTime = false; 
    } 

    return isNowWithinSleepingTime; 

} 

但這並不出於某種原因。另外,我覺得必須有更好的方法來實現比較。任何建議請!

回答

4

我使用joda時間來處理日期。所以我有這樣的事:

package org.devmaster.sample; 

import org.joda.time.DateTime; 
import org.joda.time.Interval; 

import java.util.Calendar; 
import java.util.Date; 

public final class Util { 

    public static boolean compareHrsAndMintsOnly(Date startSH, Date now, Date stopSH) { 
     DateTime start = toDateTime(startSH); 
     DateTime end = toDateTime(stopSH); 

     Interval interval = new Interval(start, end); 

     DateTime instant = toDateTime(now); 

     // now is between startSH and stopSH 
     return interval.contains(instant); 
    } 

    private static DateTime toDateTime(Date date) { 
     Calendar c = Calendar.getInstance(); 
     c.setTime(date); 
     return new DateTime(
       c.get(Calendar.YEAR), 
       c.get(Calendar.MONTH), 
       c.get(Calendar.DAY_OF_MONTH), 
       c.get(Calendar.HOUR_OF_DAY), 
       c.get(Calendar.MINUTE)); 
    } 

} 

編輯

我做了一個樣本,如果你想要玩:https://github.com/betorcs/jodatime-sample

+0

謝謝你的建議。我現在將測試它是否有效。看起來很簡單,容易理解! :) –

+0

我接受了你的答案,因爲它是最容易實現的,它是可靠的(圖書館被證明是好的)。再次感謝! –

+0

剛剛測試過。當startSH和stopSH過去時,它不起作用:/ –

2

保持簡單:

if(nowHour == stopSHhour && nowMin == stopSHmin){ 
    // do something... 

} 
+0

謝謝你的建議。但是,由於我每半小時檢查一次,因此檢查將失敗。例如,檢查可能發生在8點15分,開始睡眠時間可能是8點。它必須>或< –

2

試試這個:

Calendar startSHCalendar = Calendar.getInstance(); 
    startSHCalendar.setTime(startSH); 
    Calendar nowCalendar = Calendar.getInstance(); 
    nowCalendar.setTime(now); 
    Calendar stopSHCalendar = Calendar.getInstance(); 
    stopSHCalendar.setTime(stopSH); 

    int startSHhour = startSHCalendar.get(Calendar.HOUR_OF_DAY); 
    int startSHmin = startSHCalendar.get(Calendar.MINUTE); 
    int timeStart = startSHhour*60 + startSHmin; //this 

    int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY); 
    int nowMin = nowCalendar.get(Calendar.MINUTE); 
    int timeNow = nowHour*60 + nowMin; //this 

    int stopSHhour = stopSHCalendar.get(Calendar.HOUR_OF_DAY); 
    int stopSHmin = stopSHCalendar.get(Calendar.MINUTE); 
    int timeStop = stopSHhour*60 + stopSHmin; //this 

    if(timeStart <= timeNow && timeNow <= timeStop){ 
     //between 
    }else{ 
     //not betwwen 
    } 

/\警告,如果你兩天之間,還必須與天

希望它比較!將幫助

+0

謝謝您的建議!它似乎在我的測試過程中工作!真的很感激它! –