检查时间是否在最小和最大范围内 [英] Check if the time is within the min and max range

查看:78
本文介绍了检查时间是否在最小和最大范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这里发生了什么:如果当前时间在给定范围之间,我需要返回 true,例如:现在是 15:45:00 并且在 12:00:00(min) 和 18 之间:00:00(max),但是这个方法做错了:

I don't know what's happening here: I need to return true if the current time is between a given range, ex: now is 15:45:00 and is between 12:00:00(min) and 18:00:00(max), but this method is doing it wrong:

private boolean verifyIfInside(String max, String min){
    try {
        DateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss");

        Date date = new Date();

        String hour1 = min;
        String hour2 = max;
        String newHour = dateFormat.format(date);

        Date minHour, maxHour, nowHour;
        minHour = dateFormat.parse(hora1);
        maxHour = dateFormat.parse(hora2);
        nowHour = dateFormat.parse(newHour );

        if ((minHour.compareTo(nowHour) <= 0) && (maxHour.compareTo(nowHour) >= 0)){
            return true;
        } else {
            return false;
        }
    } catch (ParseException parseException){
        parseException.printStackTrace();
        return false;
    }
}

推荐答案

它与建议的修改一起正常工作:

It works correctly with the suggested modifications:

  • 反转最小和最大参数.
  • 使用 .before().after() 比较当前小时是否在最小小时之后和最大小时之前.
  • 要包含您可以使用的最小/最大时间边界:!nowHour.after(maxHour) &&!nowHour.before(minHour);
  • 要排除最小/最大时间边界,请改用:nowHour.after(minHour) &&nowHour.before(maxHour);
  • 如果您受限于旧版本的 java,请考虑使用@Basil Bourque 建议的适当 java.time ThreeTen-Backport 项目.否则,请使用 Java 8+ 中的最新时间 API.
  • Reverse the min and max parameters.
  • Use .before() and .after() to compare if the current hour is after min hour and before max hour.
  • To include the min/max time boundaries you can use: !nowHour.after(maxHour) && !nowHour.before(minHour);
  • To exclude the min/max time boundaries use this instead: nowHour.after(minHour) && nowHour.before(maxHour);
  • If you are constrained to an older version of java, consider using the appropriate java.time ThreeTen-Backport project as @Basil Bourque suggested. Otherwise, use the latest time API in Java 8+.

代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class scratch_21 {
    public static void main(String[] args) {
        System.out.println(verifyIfInside("14:00:00", "14:15:00"));
    }

    private static boolean verifyIfInside(String min, String max) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

            Date date = new Date();

            String hora1 = min;
            String hora2 = max;
            String newHour = dateFormat.format(date);

            Date minHour, maxHour, nowHour;
            minHour = dateFormat.parse(hora1);
            maxHour = dateFormat.parse(hora2);
            nowHour = dateFormat.parse(newHour);

            return  nowHour.after(minHour) && nowHour.before(maxHour);
        } catch (ParseException parseException) {
            parseException.printStackTrace();
            return false;
        }
    }
}

这篇关于检查时间是否在最小和最大范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆