判斷當前時間是否在時間段內java
阿新 • • 發佈:2019-01-24
/**
* 判斷當前時間是否在[startTime, endTime]區間,注意時間格式要一致
*
* @param nowTime 當前時間
* @param startTime 開始時間
* @param endTime 結束時間
* @return
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 判斷時間是否在[startTime, endTime]區間,注意時間格式要一致
* @param nowTime
* @param startTime
* @param endTime
* @return
*/
public static boolean isEffectiveDate(String nowTime, String startTime, String endTime,String dateFormat) {
DateFormat df = new SimpleDateFormat(dateFormat);
Date nowDate = null;
Date startDate = null;
Date endDate = null;
try {
nowDate = df.parse(nowTime);
startDate = df.parse(startTime);
endDate = df.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowDate.getTime() == startDate.getTime()
|| nowDate.getTime() == endDate.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowDate);
Calendar begin = Calendar.getInstance();
begin.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
* 判斷當前時間是否在[startTime, endTime]區間,注意時間格式要一致
*
* @param nowTime 當前時間
* @param startTime 開始時間
* @param endTime 結束時間
* @return
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 判斷時間是否在[startTime, endTime]區間,注意時間格式要一致
* @param nowTime
* @param startTime
* @param endTime
* @return
*/
public static boolean isEffectiveDate(String nowTime, String startTime, String endTime,String dateFormat) {
DateFormat df = new SimpleDateFormat(dateFormat);
Date nowDate = null;
Date startDate = null;
Date endDate = null;
try {
nowDate = df.parse(nowTime);
startDate = df.parse(startTime);
endDate = df.parse(endTime);
} catch (ParseException e) {
e.printStackTrace();
}
if (nowDate.getTime() == startDate.getTime()
|| nowDate.getTime() == endDate.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowDate);
Calendar begin = Calendar.getInstance();
begin.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}