2017-05-02 81 views
-4

我有幾個要求,我需要驗證,如果今天的日期是與一個條件匹配,如果真的在數據庫中創建記錄。Java檢查今天的日期是兩週週二

但條件是這樣的 - 週二週二,週二週四。 任何人都可以提供如何實現這一點?

+6

看起來你希望我們爲你寫一些代碼。儘管許多用戶願意爲遇險的編碼人員編寫代碼,但他們通常只在海報已嘗試自行解決問題時才提供幫助。證明這一努力的一個好方法是包含迄今爲止編寫的代碼,示例輸入(如果有的話),期望的輸出以及實際獲得的輸出(控制檯輸出,回溯等)。您提供的細節越多,您可能會收到的答案就越多。檢查[FAQ]和[問]。 –

+0

當然,我已經嘗試使用喬達時間和日曆代碼示例。 – hanmayya

+2

@hanmayya然後發佈您的代碼 – litelite

回答

0

private static Calendar cacheCalendar;

  public static LocalDate getNDayOfMonth(int dayweek, int nthweek, int month, int year) { 
          LocalDate d = new LocalDate(year, month, 1).withDayOfWeek(dayweek); 
          if (d.getMonthOfYear() != month) 
              d = d.plusWeeks(1); 
          return d.plusWeeks(nthweek - 1); 
      } 

      public static LocalDate getLastWeekdayOfMonth(int dayweek, int month, int year) { 
          LocalDate d = new LocalDate(year, month, 1).plusMonths(1).withDayOfWeek(dayweek); 
          if (d.getMonthOfYear() != month) 
              d = d.minusWeeks(1); 
          return d; 
      } 

      public static void main(String[] args) { 

          cacheCalendar = Calendar.getInstance(); 

          for (int i = 1; i < 13; i++) { 
              // second wednesday of oct-2011 

              LocalDate secondMonday = getNDayOfMonth(DateTimeConstants.MONDAY, 2, i, cacheCalendar.get(Calendar.YEAR)); 
              System.out.println("secondMonday= " + secondMonday); 

              LocalDate secondTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 2, i, 
                      cacheCalendar.get(Calendar.YEAR)); 
              System.out.println("secondTuesday= " + secondTuesday); 

              LocalDate monthlyTuesday = getNDayOfMonth(DateTimeConstants.TUESDAY, 1, i, 
                      cacheCalendar.get(Calendar.YEAR)); 
              System.out.println("monthlyTuesday= " + monthlyTuesday);             

              LocalDate weeklyFirstFriday = getNDayOfMonth(DateTimeConstants.FRIDAY, 1, i, 
                      cacheCalendar.get(Calendar.YEAR)); 
              System.out.println("weeklyFirstFriday= " + weeklyFirstFriday);             


          }         
      } 
相關問題