判断时间在某个时间段

来源:百度知道 编辑:UC知道 时间:2024/05/21 13:13:58
我在类写了个方法,但是,这个方法只能在9:00到15:00执行!其他时间不执行,请高手解决下
2楼的好象不行哦!如果时间为9:40,now.getHours()得到的还是9;还是会执行!

Date nowTime=new Date();
String time = nowTime.getHours() + ":" + nowTime.getMinutes();
if (time.compareTo("09:00") >= 0 && time.compareTo("15:00") <= 0) {
System.out.println("in");
} else {
System.out.println("not in");
}

这样试试

java.util.Date now = new java.util.Date();
int hour = now.getHours();//取当前时间的小时.

if( hour >= 9 && hour <= 15)
{
//你的方法
}