/**
* 判断某一时间是否在一个区间内
*
* @param sourceTime
* 时间区间,半闭合,如[10:00-20:00)
* @param curTime
* 需要判断的时间
* @return
* @throws IllegalArgumentException
* sourceTime格式 21:30-24:00
* curTime 格式 new Date()
*/
public static boolean isInTime(String sourceTime, Date curTime) {
if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
}
String[] args = sourceTime.split("-");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
try {
String curT = sdf.format(curTime);
long now = sdf.parse(curT).getTime();
long start = sdf.parse(args[0]).getTime();
long end = sdf.parse(args[1]).getTime();
if (args[1].equals("00:00")) {
args[1] = "24:00";
}
if (end < start) {
if (now >= end && now < start) {
return false;
} else {
return true;
}
}
else {
if (now >= start && now < end) {
return true;
} else {
return false;
}
}
} catch (ParseException e) {
e.printStackTrace();
throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
}
}