判断时间节点是否在给定时间之内

            获取给定时间的时分秒,全部转换为秒,然后进行判断。代码如下

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Begintime = "16:33:00";
        String Endtime = "16:35:00";
        String strDate = sdf.format(new 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));
        double strTotalS = strDateH * 3600 + strDateM * 60 + strDateS;
        
        int BeginH = Integer.parseInt(Begintime.substring(0, 2));
        int BeginM = Integer.parseInt(Begintime.substring(3, 5));
        int BeginS = Integer.parseInt(Begintime.substring(6, 8));
        double beginTotalS = BeginH * 3600 + BeginM * 60 + BeginS;

        int EndH = Integer.parseInt(Endtime.substring(0, 2));
        int EndM = Integer.parseInt(Endtime.substring(3, 5));
        int EndS = Integer.parseInt(Endtime.substring(6, 8));
        double endTotalS = EndH * 3600 + EndM * 60 + EndS;

        //当前时间小时大于开始时间而且小于结束时间
        if(beginTotalS<=strTotalS && strTotalS<=endTotalS){
            System.out.println("true");
        }else{
            System.out.println("false");
        }


(比较基础,其实思路有了,做起来就非常容易了!由于不知道网上是否有人写过类似的代码。如果有,请告知!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值