@Test
public void test() throws ParseException {
//判断当前时间是否在 21时-次日凌晨1时 之间
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("不在");
}
}
10-30
4308
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交