判断现在是否在这个时间段内


    /**
     * 判断现在是否在这个时间段内 chenjy
     *
     * @param startTime
     * @param endTime
     * @return boolean
     */
    public static boolean isInRingTime(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = sdf.parse(startTime);
            Date date2 = sdf.parse(endTime);
            if (date1.getTime() < System.currentTimeMillis() && System.currentTimeMillis() < date2.getTime()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return true;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 JavaScript 中判断当前时间是否在多个时间段内,可以先获取当前时间,然后使用条件语句进行判断。 首先,获取当前时间可以使用 JavaScript 的内置对象Date(),示例代码如下: let currentTime = new Date(); 接下来,我们可以定义多个时间段的起始时间和结束时间,以数组的形式存储。例如,我定义了两个时间段,分别是8:00-12:00和14:00-18:00,代码如下: let timeRanges = [ {start: '08:00', end: '12:00'}, {start: '14:00', end: '18:00'} ]; 然后,我们可以编写一个函数来判断当前时间是否在这些时间段内。函数中可以遍历时间段数组,每次比较当前时间是否时间段的起始时间和结束时间之间。如果在任何一个时间段内,则返回true,否则返回false。示例代码如下: function isInTimeRanges(currentTime, timeRanges) { for(let i = 0; i < timeRanges.length; i++) { let start = new Date(currentTime.toDateString() + ' ' + timeRanges[i].start); let end = new Date(currentTime.toDateString() + ' ' + timeRanges[i].end); if(currentTime >= start && currentTime <= end) { return true; } } return false; } 最后,我们可以调用这个函数来判断当前时间是否在多个时间段内。示例代码如下: if(isInTimeRanges(currentTime, timeRanges)) { console.log('当前时间在指定时间段内'); } else { console.log('当前时间不在指定时间段内'); } 通过上述方法,我们可以判断当前时间是否在多个时间段内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值