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 now = new Date();
    DateFormat dateFormat = DateFormat.getTimeInstance();//获取时分秒
    //得到当前时间的时分秒
    String nowStr =
    dateFormat.format(now);

    Date nowTime = new SimpDateFormat(format).parse(nowStr);

    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; } } }

    转载至:https://www.cnblogs.com/linjiqin/p/8119338.html

  • 相关阅读:
    application/json 四种常见的 POST 提交数据方式
    物联网应用中实时定位与轨迹回放的解决方案 – Redis的典型运用(转载)
    C#的四种Timer介绍
    用python绘制趋势图
    Python 图形界面元素
    python 异常和弹出框
    python爬虫
    python文件操作
    GUI输入数据并保存
    图形界面+声音播放
  • 原文地址:https://www.cnblogs.com/zml-java/p/9802278.html
Copyright © 2011-2022 走看看