我需要知道是否或介於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);
閱讀有關'java.util.Calendar' – Jens
嘗試date.after()和date.before() –
嘗試用一種算法,會做這樣的:'FROM日期<日期
AxelH