2014-11-05 122 views
2

我必須根據當前日期計算下一個日期。例如,今天說的是'05 -NOV-2014',用戶給了第一個星期一的輸入。所以根據用戶的輸入,如果本月的第一個星期一已經過去,那麼我必須找出下個月的第一個星期一。如果第一個星期一還沒有出現在當前月份中,那麼我必須在當月內找出日期。如何獲取下一個指定的某一天的日期

我正在使用java.util.Calendar類。

//user inputs 
int dayOfWeek = 3; // as user has provided 'Tuesday' 
int noOfWeek = 1; // as user provided 1st 

Calendar cal = Calendar.getInstance(); // getting the current instance. 
int currentDayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK); // getting the cCurrentDayOfWeek in integer. 
int currentNoOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK_IN_MONTH); // getting which week it is in the current month 


if(noOfWeek < currentNoOfWeek){ 
// the date has gone past in the current month. 
} 
else if ((noOfWeek == currentNoOfWeek) && dayOfWeek < currentDayOfWeek){ 
// though the week number is same but as the currentDayOfWeek is greater than the provided day so in this case also date has gone past in the current month. 
} 

無法繼續進行。尋求你的幫助。

+0

顯示您嘗試過的代碼。 – Jens 2014-11-05 10:01:28

+0

@Jens:我已經用代碼更新了我的問題。請幫助我。 – Shibankar 2014-11-05 10:36:31

回答

1

這是你有一個很酷的問題,這是解決方案,我想出了和我的方法:

首先,你有什麼:

public static void main(String[] args) { 
    // TODO: validate user-input 

    // Input by user: 
    int inputDayOfWeek = 3; // Tuesday 
    int inputWeekOfMonth = 2; 

    if(isInNextMonth(inputDayOfWeek, inputWeekOfMonth)){ 
     Date outputDate = calculateNextValidDate(inputDayOfWeek, inputWeekOfMonth); 

     // Do something with the outputDate 
     System.out.println(outputDate.toString()); 
    } 
} 

private static boolean isInNextMonth(int inputDayOfWeek, int inputWeekOfMonth){ 
    // Current day: 
    Calendar cal = Calendar.getInstance(); 
    int currentDayOfWeek = cal.get(Calendar.DAY_OF_WEEK); 
    int currentWeekOfMonth = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH); 

    // The date has gone past in the current month 
    // OR though it's the same week of the month, the day of the week is past the current day of the week 
    return inputWeekOfMonth < currentWeekOfMonth || ((inputWeekOfMonth == currentWeekOfMonth) && inputDayOfWeek < currentDayOfWeek); 
} 

需要注意以下幾點:我因爲在這兩種情況下你都想去下一個月,並且使它成爲一個單獨的方法(使它成爲一個單獨的方法只是我自己的偏好,所以保持事情結構和組織)。

我注意到的另一件事是你的if和else-if中有一個錯誤。它應該是noOfWeek < currentNoOfWeek而不是noOfWeek > currentNoOfWeek((noOfWeek == currentNoOfWeek) && dayOfWeek > currentDayOfWeek)而不是((noOfWeek == currentNoOfWeek) && dayOfWeek < currentDayOfWeek)<>是相反的)。


現在calculateNextValidDate方法,這是你的問題所在。我的方法如下:

  • 開始在下個月
  • 圍棋的第一天到這個月的正確周
  • 然後去這個星期的正確日子

這給了我下面的代碼:

private static Date calculateNextValidDate(int inputDayOfWeek, int inputWeekOfMonth){ 
    // Set the first day of the next month as starting position: 
    Calendar cal = Calendar.getInstance(); 
    cal.add(Calendar.MONTH, 1); 
    cal.set(Calendar.DAY_OF_MONTH, 1); 

    // Now first go to the correct week of this month 
    int weekOfNextMonth = 1; 
    while(weekOfNextMonth < inputWeekOfMonth){ 
     // Raise by a week 
     cal.add(Calendar.DAY_OF_MONTH, 7); 
     weekOfNextMonth++; 
    } 

    // Now that we have the correct week of this month, 
    // we get the correct day 
    while(cal.get(Calendar.DAY_OF_WEEK) != inputDayOfWeek){ 
     // Raise by a day 
     cal.add(Calendar.DAY_OF_MONTH, 1); 
    } 

    return cal.getTime(); 
} 

這段代碼給了我以下輸出(2014年11月5日星期三-wi第3 [Tuesday]2作爲輸入):

Tue Dec 09 17:05:42 CET 2014 

還要注意// TODO:我已經在這個支柱的所述第一代碼部分的主方法添加。如果用戶輸入無效(例如,如周負面或dayOfMonth),則可能會經歷多次while循環。我把它留給你來驗證用戶輸入。

+0

非常感謝。你已經救了我的一天。乾杯.. !! – Shibankar 2014-11-05 20:00:26

0

這是一個想法。有一天,每次迭代(即循環包裝)繼續滾動Calendar實例[cal.add(DAY,1)],每次檢查您在指定爲輸入的Day/Week中。一旦滿足條件,完成並提取日期。

此外,在「cal」變量聲明之後,將cal.get()的調用替換爲Calendar.getInstance()。get()。

相關問題