Java判断一个时间是否在另一个时间段内

需求:当时间在凌晨0点至0点5分之间程序不执行。

  也就是实现判断当前时间点是否在00:00:00至00:05:00之间

  方法:

  Java代码 :

  /**

  * 判断时间是否在时间段内 *

  * @param date

  * 当前时间 yyyy-MM-dd HH:mm:ss

  * @param strDateBegin

  * 开始时间 00:00:00

  * @param strDateEnd

  * 结束时间 00:05:00

  * @return

  */

  public static boolean isInDate(Date date, String strDateBegin,

  String strDateEnd) {

  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  String strDate = sdf.format(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));

  // 截取开始时间时分秒

  int strDateBeginH = Integer.parseInt(strDateBegin.substring(0, 2));

  int strDateBeginM = Integer.parseInt(strDateBegin.substring(3, 5));

  int strDateBeginS = Integer.parseInt(strDateBegin.substring(6, 8));

  // 截取结束时间时分秒

  int strDateEndH = Integer.parseInt(strDateEnd.substring(0, 2));

  int strDateEndM = Integer.parseInt(strDateEnd.substring(3, 5));

  int strDateEndS = Integer.parseInt(strDateEnd.substring(6, 8));

  if ((strDateH >= strDateBeginH && strDateH <= strDateEndH)) {

  // 当前时间小时数在开始时间和结束时间小时数之间

  if (strDateH > strDateBeginH && strDateH < strDateEndH) {

  return true;

  // 当前时间小时数等于开始时间小时数,分钟数在开始和结束之间

  } else if (strDateH == strDateBeginH && strDateM >= strDateBeginM

  && strDateM <= strDateEndM) {

  return true;

  // 当前时间小时数等于开始时间小时数,分钟数等于开始时间分钟数,秒数在开始和结束之间

  } else if (strDateH == strDateBeginH && strDateM == strDateBeginM

  && strDateS >= strDateBeginS && strDateS <= strDateEndS) {

  return true;

  }

  // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数小等于结束时间分钟数

  else if (strDateH >= strDateBeginH && strDateH == strDateEndH

  && strDateM <= strDateEndM) {

  return true;

  // 当前时间小时数大等于开始时间小时数,等于结束时间小时数,分钟数等于结束时间分钟数,秒数小等于结束时间秒数

  } else if (strDateH >= strDateBeginH && strDateH == strDateEndH

  && strDateM == strDateEndM && strDateS <= strDateEndS) {

  return true;

  } else {

  return false;

  }

  } else {

  return false;

  }

  }

转载于:https://www.cnblogs.com/kaige123/p/5709165.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值