2017-10-13 63 views
-7

我需要知道是否或介於2個日期之間。情況下,如果我有9月29日和10月1日,這應該返回true,因爲這將覆蓋9月30日\如何檢查某個DAY是否在Java中的兩個日期之間?

我當前的代碼:

SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
Date min = formatter.parse("10-29-2017"); 
Date max = formatter.parse("11-1-2017"); 
boolean checker = false; 
Calendar cal = Calendar.getInstance(); 
cal.setTime(min); 
int minDay = cal.get(Calendar.DAY_OF_MONTH); 
cal.setTime(max); 
int maxDay = cal.get(Calendar.DAY_OF_MONTH); 
for(int i = minDay+1;i<=maxDay;i++){ 
    if(i==15 || i ==30){ 
     checker = true; 
    } 
} 
System.out.println(checker); 

這將是除非最小和最大的樣準確上面,它會返回false。

我明白了。感謝所有的提示。 終極密碼:

 SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
     Date min = formatter.parse("10-29-2017"); 
     Date max = formatter.parse("11-1-2017"); 
     boolean checker = false; 
     Calendar cal = Calendar.getInstance(); 
     Calendar cal1 = Calendar.getInstance(); 
     cal.setTime(min); 
//  int minDay = cal.get(Calendar.DAY_OF_MONTH); 
     cal1.setTime(max); 
//  int maxDay = cal.get(Calendar.DAY_OF_MONTH); 
     while(cal.before(cal1) && !checker){ 
      cal.add(Calendar.DAY_OF_MONTH, 1); 
      if(cal.get(Calendar.DAY_OF_MONTH)==15 || cal.get(Calendar.DAY_OF_MONTH)==30){ 
       checker = true; 
      } 
      System.out.println(cal.getTime()); 
     } 
     System.out.println(checker); 
+1

閱讀有關'java.util.Calendar' – Jens

+1

嘗試date.after()和date.before() –

+0

嘗試用一種算法,會做這樣的:'FROM日期<日期 AxelH

回答

0

這是如何檢查的日子已經過去了或示例中未

static boolean checkDate(int day){ 
    DateFormat dateFormat = new SimpleDateFormat("dd"); 
    Calendar c = Calendar.getInstance(); 
    int daysInMonth = c.getActualMaximum(Calendar.DAY_OF_MONTH); 
    return day <= daysInMonth; 
} 
+0

如果我理解正確,這隻能檢查其月內的日期,如果它跨越到另一個月 –

+0

'new Date()'是今天的日期。這個問題不關心今天的日期是什麼。 – DodgyCodeException

+0

@DodgyCodeException我實際上意識到這條線是多餘的,刪除。 – FrankK

0

使用這樣的事情。

SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
    Date min = formatter.parse("10-29-2017"); 
    Date max = formatter.parse("11-1-2017"); 
    Date d = new Date(); 
    if(d.after(min) && d.before(max)){ 
     System.out.println("in between"); 
    }else{ 
     System.out.println("not in between"); 
    } 
+0

'new Date()'是今天的日期。這個問題不關心今天的日期是什麼。 – DodgyCodeException

+0

似乎對我來說,OP可以理解他應該給'd'分配日期來檢查。 –

1
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy"); 
     Date min = formatter.parse("10-29-2017"); 
     Date max = formatter.parse("11-1-2017"); 
     boolean checker = false; 
     Calendar cal = Calendar.getInstance(); 
     Calendar cal1 = Calendar.getInstance(); 
     cal.setTime(min); 
//  int minDay = cal.get(Calendar.DAY_OF_MONTH); 
     cal1.setTime(max); 
//  int maxDay = cal.get(Calendar.DAY_OF_MONTH); 
     while(!checker && cal.before(cal1)){ 
      cal.add(Calendar.DAY_OF_MONTH, 1); 
      if(cal.get(Calendar.DAY_OF_MONTH)==15 || cal.get(Calendar.DAY_OF_MONTH)==30){ 
       checker = true; 
      } 
      System.out.println(cal.getTime()); 
     } 
     System.out.println(checker); 
+0

好,雖然我會把'!checker && cal.before(cal1)',因爲檢查'checker'比調用'Calendar.before()'快。在這種情況下,它不會產生任何可測量的性能差異,但在更復雜的事情之前進入檢查簡單事物是一種好習慣。 – DodgyCodeException

+0

@DodgyCodeException完成編輯。謝謝你,先生教你用自己的方式。 –

相關問題