@Test
public void test() throws ParseException {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DATE);
Date date = new Date(calendar.get(Calendar.YEAR), (Calendar.MONTH) + 1, Calendar.DATE);
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date earlyStart = format.parse(year+"-"+month+"-"+day + " " + "0:00:00");
Date earlyEnd = format.parse(year+"-"+month+"-"+day + " " + "01:00:00");
Date delayStart = format.parse(year+"-"+month+"-"+day + " " + "21:00:00");
Date delayEnd = format.parse(year+"-"+month+"-"+day + " " + "23:59:59");
Date nextStart = format.parse(year+"-"+month+"-"+(day+1) + " " + "0:00:00");
Date nextEnd = format.parse(year+"-"+month+"-"+(day+1) + " " + "01:00:00");
System.out.println(earlyStart);
System.out.println(earlyEnd);
System.out.println(delayStart);
System.out.println(delayEnd);
Date now = new Date();
System.out.println(now);
if((now.after(earlyStart) && now.before(earlyEnd)) ||
(now.after(delayStart) && now.before(delayEnd)) ||
(now.after(nextStart) && now.before(nextEnd))){
System.out.println("在");
}else {
System.out.println("不在");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c1db10948f709959e03aab48ed962710.png)