2010-10-18 24 views
0

這是一個非常簡單的請求,但我不太確定生成這兩個值的最簡單/最有效的方法。需要Java任務的兩個Date()值

我需要編寫一個腳本來檢查給定值是否在兩個值之間。我很清楚這是如何在SQL中完成的。

我需要的值的方式是somethign類似於以下。

Date testValue = new Date()  //This represents the value we are testing 

Date beginningOfDay = ....  //This value would represent the date for 
            testValue at 12:00am 

Date endOfDay = ...    //This value would represent the date for 
            testValue at 11:59:59pm 

同樣,Java Date()類型可能不是最好的做法。最後我只需要生成三個值,我可以說

if testValue is after beginningOfDay && testValue is before endOfDay 
    //do logic 
+1

這有幫助嗎? http://stackoverflow.com/questions/494180/java-how-do-i-check-if-a-date-is-within-a-certain-range – Crag 2010-10-18 18:52:23

回答

5

我個人使用日曆對象這一點。例如:

Date testDate = ??? //Replace with whatever source you are using 
Calendar testDateCalendar = Calendar.getInstance(); 
testDateCalendar.setTime(testDate); 

Date today = new Date(); 
Calendar endOfDay = Calendar.getInstance(); //Initiates to current time 
endOfDay.setTime(today); 
endOfDay.set(Calendar.HOUR_OF_DAY, 23); 
endOfDay.set(Calendar.MINUTE, 59); 
endOfDay.set(Calendar.SECOND, 59); 

Calendar startOfDay = Calendar.getInstance(); 
startOfDay.setTime(today); 
startOfDay.set(Calendar.HOUR_OF_DAY, 0); 
startOfDay.set(Calendar.MINUTE, 0); 
startOfDay.set(Calendar.SECOND, 0); 

if (startOfDay.before(testDateCalendar) && endOfDay.after(testDateCalendar)) 
{ 
//Whatever 
} else { 
//Failure 
} 
+1

雖然我很欣賞Java中的Calendar類,但我討厭它創建的雜波 – Faheem 2010-10-18 18:59:23

+1

如果兩個Calendar.getInstance之間的日期發生變化(即午夜),這將失敗。 – 2010-10-18 19:05:15

+1

總是會導致失敗:如果參數不是日曆的實例,'Calendar.before'和'Calendar.after'總是返回'false'。 Doc:「......當且僅當'when是Calendar實例時,否則該方法返回false。」 – 2010-10-18 20:59:37

2

您可以使用日曆對象做到這一點,順便說一下,你沒界限在你的問題檢查的方式是錯誤的(您日期日期後的前/匹配,並且仍然被認爲是在範圍內)。以下顯示一個日期是否在一年中的某一天。它假定的日期時間的時區,以檢查和白天是平等的,沒有時間調整發生了:

Date dateTime=... 
Date day=... 

// This is the date we're going to do a range check on 
Calendar calDateTime=Calendar.getInstance(); 

calDateTime.setTime(dateTime); 

// This is the day from which we will get the month/day/year to which 
// we will compare it 
Calendar calDay=Calendar.getInstance(); 

calDay.setTime(day); 

// Calculate the start of day time 
Calendar beginningOfDay=Calendar.getInstance(); 

beginningOfDay.set(calDay.Get(Calendar.YEAR), 
        calDay.Get(Calendar.MONTH), 
        calDay.Get(Calendar.DAY_OF_MONTH), 
        0, // hours 
        0, // minutes 
        0); // seconds 

// Calculate the end of day time 
Calendar endOfDay=Calendar.getInstance(); 

endOfDay.set(calDay.Get(Calendar.YEAR), 
      calDay.Get(Calendar.MONTH), 
      calDay.Get(Calendar.DAY_OF_MONTH), 
      23, // hours 
      59, // minutes 
      59); // seconds 

// Now, to test your date. 
// Note: You forgot about the possibility of your test date matching either 
//  the beginning of the day or the end of the day. The accepted answer 
//  got this range check wrong, as well. 
if ((beginningOfDay.before(calDateTime) && endOfDay.after(calDateTime)) || 
    beginningOfDay.equals(calDateTime) || endOfDay.equals(calDateTime)) 
{ 
    // Date is in range... 
} 

這可以進一步簡化爲:

Date dateTime=... 
Date day=... 

// This is the date we're going to do a range check on 
Calendar calDateTime=Calendar.getInstance(); 

calDateTime.setTime(dateTime); 

// This is the day from which we will get the month/day/year to which 
// we will compare it 
Calendar calDay=Calendar.getInstance(); 

calDay.setTime(day); 

if (calDateTime.get(YEAR)==calDay.get(YEAR) && 
    calDateTime.get(MONTH)==calDay.get(MONTH) && 
    calDateTime.get(DAY_OF_YEAR)==calDay.get(DAY_OF_YEAR)) 
{ 
    // Date is in range 
} 
0

這裏的東西我用在我自己的代碼中確定某個文件是否在某一天被修改。就像這個主題中的其他答案一樣,我使用了日曆。

// Get modified date of the current file 
// and today's date 
Calendar today = Calendar.getInstance(); 
Calendar modDate = Calendar.getInstance(); 
Date date = new Date(file.lastModified()); 
modDate.setTime(date); 
// Convert dates to integers 
int modDay = modDate.get(Calendar.DAY_OF_YEAR); 
int todayDay = today.get(Calendar.DAY_OF_YEAR); 
if (modDay == todayDay) { 
    // Do stuff 
} 

這可能更接近您要查找的內容,因爲您只需查看該事件是否在某一天發生。