博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java判断一个时间是否在另一个时间段内
阅读量:6687 次
发布时间:2019-06-25

本文共 2029 字,大约阅读时间需要 6 分钟。

hot3.png

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

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

  方法:

  Java代码 :

  /**

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

  * date

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

  * strDateBegin

  * 开始时间 00:00:00

  * strDateEnd

* 结束时间 00:05:00

  *

  */

  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://my.oschina.net/kaige123/blog/718709

你可能感兴趣的文章
Javascript如何实现GPU加速?
查看>>
次世代的会话管理项目 Spring Session
查看>>
SQL SERVER 2008安全配置
查看>>
Json hijacking/Json劫持漏洞
查看>>
算法知识梳理(5) 数组第二部分
查看>>
多页应用增量更新静态资源Webpack打包方案
查看>>
ionic V3.3开发踩坑集锦
查看>>
Realm入门指北
查看>>
彻底搞懂Bean加载
查看>>
iOS之传值
查看>>
技术blog 迁移
查看>>
linux 定时器怎么用? crontab 基础
查看>>
React Native - Image
查看>>
Docker和宿主机操作系统文件目录互相隔离的实现原理
查看>>
小程序踩坑系列一
查看>>
探索webpack热更新对代码打包结果的影响(二)
查看>>
微信小程序_豆瓣电影
查看>>
记一次网络模块的小规模重构
查看>>
FMI-人工智能&大数据高峰论坛(深圳站)
查看>>
区块链简单研读笔记
查看>>