需求:当时间在凌晨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;

  }

  }

Java判断一个时间是否在另一个时间段内的更多相关文章

  1. Java判断当前时间是否在某一时间段内

    今天有一个任务,判断现在的时间是否在某一个时间段内 遇到的第一个问题 Date类获取日期时间大的方法失效了 问题描述: 在学习Date类时,习惯性的用get方法调用Date()的年月日,发现不怎么好用 ...

  2. c# 判断当前时间是否在某一时间段内

    //获取当前系统时间并判断是否为服务时间 TimeSpan nowDt = DateTime.Now.TimeOfDay; TimeSpan workStartDT = DateTime.Parse( ...

  3. js 判断当前时间是否处于某个一个时间段内

    js 判断当前时间(或者所选时间)是否在某一时间段 我们可以使用 jutils - JavaScript常用函数库的 isDuringDate 函数来实现 传入 beginDateStr (开始时间) ...

  4. c# 判断时间是否在 某一时间段内

    protected bool getTimeSpan(string timeStr) { //判断当前时间是否在工作时间段内 string _strWorkingDayAM = "08:30 ...

  5. 在Java中如何设置一个定时任务,在每天的一个时间点自动执行一个特定的程序

    Quartz定时机制 首先导入jar包到程序内 quartz-all-1.6.0.jar 然后创建一个XML TimeConfig.xml 名字可以自己定义 <?xml version=&quo ...

  6. JAVA判断当前时间是上午am还是下午pm

    //结果为"0"是上午 结果为"1"是下午 public class GregorianTest { public static void main(Strin ...

  7. Java获取指定时间的毫秒值的方法

    有以下两种方法获取指定时间的毫秒值: 1.Calendar类 先由getInstance获取Calendar对象,然后用clear方法将时间重置为(1970.1.1 00:00:00),接下来用set ...

  8. Java 8 日期时间API

    Java 8一个新增的重要特性就是引入了新的时间和日期API,它们被包含在java.time包中.借助新的时间和日期API可以以更简洁的方法处理时间和日期; 在介绍本篇文章内容之前,我们先来讨论Jav ...

  9. Java判断一个时间是否在时间区间内

    package com.liying.tiger.test; import java.text.ParseException; import java.text.SimpleDateFormat; i ...

随机推荐

  1. myeclipse6.5中使用Alt+/不自动提示的修改

    转载自:http://www.cnblogs.com/zhangnanblog/archive/2011/11/10/2244960.html 最近把MyEclipse8.5降到了MyEclipse6 ...

  2. 搭建Maven工程的时候,做单元测试,报ClassNotFoundException

    搭建Maven工程的时候报错 问题原因是在spring.xml中配置的  classpath:config.properties  没有在工程中创建.

  3. 引水入城(codevs 1066)

    题目描述 Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政 区划十分特殊,刚好构成一个N行M列的矩形,如上图所示,其中每个格子都代表一座城 市,每座 ...

  4. 封装自己的ajax函数

    url为具体的url地址, onsuccess为正常返回时的结果, onfail为错误返回时的结果 function MyAjax(url,onsuccess,onfail) { var xhr = ...

  5. 针对不同包之间的action跳转,怎么配置?

    例如一下的例子:两个包,如何跳转 <struts>           <constant name="struts.enable.DynamicMethodInvocat ...

  6. 一、HTML和CSS基础--HTML+CSS基础课程--第1部分

    第一章 HTML介绍 Html和CSS的关系 1. HTML是网页内容的载体.内容就是网页制作者放在页面上想要让用户浏览的信息,可以包含文字.图片.视频等. 2. CSS样式是表现.就像网页的外衣.比 ...

  7. SQL表格

    LAMP - Linux  Apache MySQL PHP MySQL - 三个层次:文件层次,服务层次,界面 常用的数据类型:int 整数float double decimal 小数varcha ...

  8. 菜鸟学Linux命令:cat命令 查看文件内容

    cat命令的用途是连接文件或标准输入并打印. 这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. Linux下查看文件内容的方式很多:vi ...

  9. Xamarin.Android开发实践(十)

    Xamarin.Android之SQLiteOpenHelper 一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数 据库帮助我们存储离线数据,以便在用户未使用网络 ...

  10. Mac OS X 上的安装nsq并使用

    安装: brew install nsq 使用: The following steps will run a small NSQ cluster on your local machine and ...