Java 判断一个时间是否在另一个时间段内
需求:当时间在凌晨0点至0点5分之间程序不执行。
也就是实现判断当前时间点是否在00:00:00至00:05:00之间
方法:
Java代码:
/**
*判断时间是否在时间段内*
*@paramdate
*当前时间yyyy-MM-ddHH:mm:ss
*@paramstrDateBegin
*开始时间00:00:00
*@paramstrDateEnd
*结束时间00:05:00
*@return
*/
publicstaticbooleanisInDate(Datedate,StringstrDateBegin,
StringstrDateEnd){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
StringstrDate=sdf.format(date);
//截取当前时间时分秒
intstrDateH=Integer.parseInt(strDate.substring(11,13));
intstrDateM=Integer.parseInt(strDate.substring(14,16));
intstrDateS=Integer.parseInt(strDate.substring(17,19));
//截取开始时间时分秒
intstrDateBeginH=Integer.parseInt(strDateBegin.substring(0,2));
intstrDateBeginM=Integer.parseInt(strDateBegin.substring(3,5));
intstrDateBeginS=Integer.parseInt(strDateBegin.substring(6,8));
//截取结束时间时分秒
intstrDateEndH=Integer.parseInt(strDateEnd.substring(0,2));
intstrDateEndM=Integer.parseInt(strDateEnd.substring(3,5));
intstrDateEndS=Integer.parseInt(strDateEnd.substring(6,8));
if((strDateH>=strDateBeginH&&strDateH<=strDateEndH)){
//当前时间小时数在开始时间和结束时间小时数之间
if(strDateH>strDateBeginH&&strDateH<strDateEndH){
returntrue;
//当前时间小时数等于开始时间小时数,分钟数在开始和结束之间
}elseif(strDateH==strDateBeginH&&strDateM>=strDateBeginM
&&strDateM<=strDateEndM){
returntrue;
//当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间
}elseif(strDateH==strDateBeginH&&strDateM==strDateBeginM
&&strDateS>=strDateBeginS&&strDateS<=strDateEndS){
returntrue;
}
//当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数
elseif(strDateH>=strDateBeginH&&strDateH==strDateEndH
&&strDateM<=strDateEndM){
returntrue;
//当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数
}elseif(strDateH>=strDateBeginH&&strDateH==strDateEndH
&&strDateM==strDateEndM&&strDateS<=strDateEndS){
returntrue;
}else{
returnfalse;
}
}else{
returnfalse;
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!