参考文献:
Java语言判断当前时间在时间范围内_java判断时间区间-CSDN博客
package com.itheima.method2;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date startTime = ft.parse("2019-06-05 03:26:54");
Date endTime = ft.parse("2019-06-09 03:26:54");
Date nowTime = new Date();
boolean effectiveDate = isEffectiveDate(nowTime, startTime, endTime);
if (effectiveDate) {
System.out.println("当前时间在范围内");
}else {
System.out.println("当前时间在不在范围内");
}
}
/**
* 判断当前时间是否在时间范围内
*
* @param nowTime
* @param startTime
* @param endTime
* @return boolean
* @author zxzhang
* @date 2020/3/16
*/
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;
}
}
}