判断是否在某一个时间段内的处理

时间:2020-12-23 17:17:58

前段时间项目中有这样的一个需求:在工作日内的早上 09:30-11:30和下午13:00-15:00这2各时间段内就不需要检测版本更新。反之则反。

其中这有2个问题需要处理:我们怎么知道当前时间是否在这2个时间段内并且是在工作日内(周一到周五)。显而易见。我们必须先获取日期在根据日期判断是否是周一到周五,然后在获取当前时间在判断是否在这个时间段内。代码如下:

/**
* <pre>
* 根据指定的日期字符串获取星期几
* </pre>
*
* @param strDate
* 指定的日期字符串(yyyy-MM-dd 或 yyyy/MM/dd)
* @return week
* 星期几(MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY)
*/
public static String getWeekByDateStr(String strDate) {
int year = Integer.parseInt(strDate.substring(0, 4));
int month = Integer.parseInt(strDate.substring(5, 7));
int day = Integer.parseInt(strDate.substring(8, 10));

Calendar c = Calendar.getInstance();

c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.DAY_OF_MONTH, day);

String week = "";
int weekIndex = c.get(Calendar.DAY_OF_WEEK);

switch (weekIndex) {
case 1:
week = "星期日";
break;
case 2:
week = "星期一";
break;
case 3:
week = "星期二";
break;
case 4:
week = "星期三";
break;
case 5:
week = "星期四";
break;
case 6:
week = "星期五";
break;
case 7:
week = "星期六";
break;
}
return week;
}

/**
* 检测版本时间 09:30-11:30
* 是否是检测版本时间以内
* @return true 则是检测版本时间内, false 为检测版本时间外
*/
public static boolean isATime(){
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK);
if(week==1 || week == 7)
return false;

long spe1 = getSpeDate(9, 30, 0, 0); // 09:30 分时时间
long spe2 = getSpeDate(11, 30, 0, 0); // 11:30 分时时间


return System.currentTimeMillis() > spe1
&& System.currentTimeMillis() < spe2;

}

/**
* 检测版本时间 13:00-15:00
* 是否是检测版本时间以内
* @return true 则是检测版本时间内, false 为检测版本时间外
*/
public static boolean isBTime(){
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK);
if(week==1 || week == 7)
return false;

long spe1 = getSpeDate(13, 00, 0, 0); // 13:00 分时时间
long spe2 = getSpeDate(15, 00, 0, 0); // 15:00 分时时间


return System.currentTimeMillis() > spe1
&& System.currentTimeMillis() < spe2;

}




/***
* 获取指定一天中指定的时间的时间戳
*
* @param hour
* @param minute
* @param second
* @param milliSecond
* @return
*/
public static long getSpeDate(int hour, int minute, int second,
int milliSecond) {

Calendar cal = Calendar.getInstance();

cal.set(Calendar.HOUR_OF_DAY, hour);

cal.set(Calendar.SECOND, second);

cal.set(Calendar.MINUTE, minute);

cal.set(Calendar.MILLISECOND, milliSecond);

return cal.getTimeInMillis();

}

调用如下:

 Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dtStr = sdf.format(now);
if (TimeUtil.getWeekByDateStr(dtStr).equals("星期六") || TimeUtil.getWeekByDateStr(dtStr).equals("星期日")) {
LogUtil.e("Protocols.AMMIDHQ_BBH return-------------发送请求");
}else {
if (TimeUtil.isATime() == true && TimeUtil.isBTime() == false) {
LogUtil.e("Protocols.AMMIDHQ_BBH return-------------在当前时间段内什么都不做");

}else if(TimeUtil.isATime() == false && TimeUtil.isBTime() == true){
LogUtil.e("Protocols.AMMIDHQ_BBH return-------------在当前时间段内什么都不做");
}else {
LogUtil.e("Protocols.AMMIDHQ_BBH return-------------发送请求");
}

}

当然大家可以把代码封装下,作为公共方法方便使用