获取给定时间的时分秒,全部转换为秒,然后进行判断。代码如下
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String Begintime = "16:33:00";
String Endtime = "16:35:00";
String strDate = sdf.format(new Date());
int strDateH = Integer.parseInt(strDate.substring(11, 13));
int strDateM = Integer.parseInt(strDate.substring(14, 16));
int strDateS = Integer.parseInt(strDate.substring(17, 19));
double strTotalS = strDateH * 3600 + strDateM * 60 + strDateS;
int BeginH = Integer.parseInt(Begintime.substring(0, 2));
int BeginM = Integer.parseInt(Begintime.substring(3, 5));
int BeginS = Integer.parseInt(Begintime.substring(6, 8));
double beginTotalS = BeginH * 3600 + BeginM * 60 + BeginS;
int EndH = Integer.parseInt(Endtime.substring(0, 2));
int EndM = Integer.parseInt(Endtime.substring(3, 5));
int EndS = Integer.parseInt(Endtime.substring(6, 8));
double endTotalS = EndH * 3600 + EndM * 60 + EndS;
//当前时间小时大于开始时间而且小于结束时间
if(beginTotalS<=strTotalS && strTotalS<=endTotalS){
System.out.println("true");
}else{
System.out.println("false");
}
(比较基础,其实思路有了,做起来就非常容易了!由于不知道网上是否有人写过类似的代码。如果有,请告知!)