查看当前时间是否落在Java当前日期的特定时间范围内 [英] See if the current time falls within a specific range of time in the current day in Java

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

问题描述

我相信在1000个不同的地方完成了1000次。问题是我想知道,如果有更好的/标准/更快的方法来检查当前的时间是否在 hh:mm:ss 格式中给出的两个时间值之间。例如,我的大业务逻辑不应该在$ code> 18:00:00和18:30:00之间之间运行。所以这里是我想到的:

  public static boolean isCurrentTimeBetween(String starthhmmss,String endhhmmss)throws ParseException {
DateFormat hhmmssFormat = new SimpleDateFormat(yyyyMMddhh:mm:ss);
Date now = new Date();
String yyyMMdd = hhmmssFormat.format(now).substring(0,8);

return(hhmmssFormat.parse(yyyMMdd + starthhmmss).before(now)&&
hhmmssFormat.parse(yyyMMdd + endhhmmss).after(now));
}

示例测试用例:

  String doNotRunBetween =18:00:00,18:30:00; //从props文件中读取
String [] hhmmss = downTime.split( );
if(isCurrentTimeBetween(hhmmss [0],hhmmss [1])){
System.out.println(NOT OK TO RUN);
} else {
System.out.println(OK To RUN);
}

我正在寻找的是更好的代码




  • 在表演中


  • 正确



我不寻找




  • 第三方库

  • 异常处理辩论

  • 变量命名约定

  • 方法修饰符问题


解决方案

这是你应该做的所有事情,这种方法是松散耦合的输入和高度一致的。

  boolean isNowBetweenDateTime(final Date,final Date e)
{
final Date now = new Date();
return now.after(s)&&现在(e)
}

如何获取开始和结束的Date对象与其进行比较无关紧要。您正在使事情的方式比您需要通过 String 表示更复杂。



这是一个更好的方式获取开始和结束日期,再次松散耦合并高度一致。

 私人日期DateFromHourMinSec(final String hhmmss)
{
if(hhmmss.matches(^ [0-2] [0-9]:[0-5] [0-9]:[0-5] [0-9] $))
{
final String [] hms = hhmmss.split(:);
final GregorianCalendar gc = new GregorianCalendar();
gc.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hms [0]));
gc.set(Calendar.MINUTE,Integer.parseInt(hms [1]));
gc.set(Calendar.SECOND,Integer.parseInt(hms [2]));
gc.set(Calendar.MILLISECOND,0);
return gc.getTime();
}
else
{
throw new IllegalArgumentException(hhmmss +不是有效时间,期望HH:MM:SS格式);
}
}

现在你可以做两个很好的命名方法调用,自我记录。


I am sure this was done 1000 times in 1000 different places. The question is I want to know if there is a better/standard/faster way to check if current "time" is between two time values given in hh:mm:ss format. For example, my big business logic should not run between 18:00:00 and 18:30:00. So here is what I had in mind:

 public static  boolean isCurrentTimeBetween(String starthhmmss, String endhhmmss) throws ParseException{
  DateFormat hhmmssFormat = new SimpleDateFormat("yyyyMMddhh:mm:ss");
  Date now = new Date();
  String yyyMMdd = hhmmssFormat.format(now).substring(0, 8);

  return(hhmmssFormat.parse(yyyMMdd+starthhmmss).before(now) &&
    hhmmssFormat.parse(yyyMMdd+endhhmmss).after(now));
 }

Example test case:

  String doNotRunBetween="18:00:00,18:30:00";//read from props file
  String[] hhmmss = downTime.split(",");
  if(isCurrentTimeBetween(hhmmss[0], hhmmss[1])){
   System.out.println("NOT OK TO RUN");
  }else{
   System.out.println("OK TO RUN");
  }

What I am looking for is code that is better

  • in performance
  • in looks
  • in correctness

What I am not looking for

  • third-party libraries
  • Exception handling debate
  • variable naming conventions
  • method modifier issues

解决方案

this is all you should need to do, this method is loosely coupled from the input and highly coherent.

boolean isNowBetweenDateTime(final Date s, final Date e)
{
    final Date now = new Date();
    return now.after(s) && now.before(e);
}

how you get the Date objects for start and end is irrelevant to comparing them. You are making things way more complicated than you need to with passing String representations around.

Here is a better way to get the start and end dates, again loosely coupled and highly coherent.

private Date dateFromHourMinSec(final String hhmmss)
{
    if (hhmmss.matches("^[0-2][0-9]:[0-5][0-9]:[0-5][0-9]$"))
    {
        final String[] hms = hhmmss.split(":");
        final GregorianCalendar gc = new GregorianCalendar();
        gc.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hms[0]));
        gc.set(Calendar.MINUTE, Integer.parseInt(hms[1]));
        gc.set(Calendar.SECOND, Integer.parseInt(hms[2]));
        gc.set(Calendar.MILLISECOND, 0);
        return gc.getTime();
    }
    else
    {
        throw new IllegalArgumentException(hhmmss + " is not a valid time, expecting HH:MM:SS format");
    }
}

Now you can make two well named method calls that will be pretty self documenting.

这篇关于查看当前时间是否落在Java当前日期的特定时间范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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