zoukankan      html  css  js  c++  java
  • JAVA判断时间是否在时间区间内

    package com.liying.tiger.test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class Test {
        public static void main(String[] args) throws ParseException {
            String format = "HH:mm:ss";
            Date nowTime = new SimpleDateFormat(format).parse("09:27:00");
            Date startTime = new SimpleDateFormat(format).parse("09:27:00");
            Date endTime = new SimpleDateFormat(format).parse("09:27:59");
            System.out.println(isEffectiveDate(nowTime, startTime, endTime));
        }
    
        /**
         * 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
         * 
         * @param nowTime 当前时间
         * @param startTime 开始时间
         * @param endTime 结束时间
         * @return
         * @author jqlin
         */
        public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
            if (nowTime.getTime() == startTime.getTime()
                    || nowTime.getTime() == endTime.getTime()) {
                return true;
            }
    
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
    
            Calendar begin = Calendar.getInstance();
            begin.setTime(startTime);
    
            Calendar end = Calendar.getInstance();
            end.setTime(endTime);
    
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        }
    }

     JAVA8:

    //构造时间  00:20:55
            LocalTime time1 = LocalTime.of(8,20,55);
            LocalTime time3 = LocalTime.of(15,20,55);
            LocalTime time4 = LocalTime.of(18,10,55);
    
            if (time1.isBefore(time3) && time4.isAfter(time3)){
                System.out.println("在时间区间之内!");
            }
  • 相关阅读:
    从0开始的Python学习013编写一个Python脚本
    Spring Cloud Eureka基本概述
    RocketMQ入门案例
    Quartz使用记录总结
    Spring Boot 1.5升级2.1 主要问题汇总
    Eureka客户端注册多网卡下IP选择问题
    Spring Security之动态配置资源权限
    代码规范问题
    Spring Security + OAuth系统环境搭建(一)
    Java并发编程之AQS
  • 原文地址:https://www.cnblogs.com/huanghuanghui/p/9870654.html
Copyright © 2011-2022 走看看