爪哇。检查时间范围。如何?

发布于 2024-12-06 05:18:20 字数 483 浏览 2 评论 0原文

例如,我的输入参数格式如下:“04:00-06:00”或“23:00-24:00”。参数类型 - String

在我的方法中,我必须检查输入参数中的时间范围不是在当前时间之前。我怎样才能做到呢?

更多详情:

输入时间范围:“12:00-15:00

当前时间:16:00

在这种情况下,方法必须返回false

另一个例子:

输入时间范围:“10:30-12:10

当前时间:09:51

方法必须返回true

你能给我一些想法或算法吗?我怎样才能实现这个方法?

For example, I have input parameter this format: "04:00-06:00" or "23:00-24:00". Type of parameter - String.

And in my method I must check, that time range in input parameter NOT before current time. How I can do it?

More details:

input time range: "12:00-15:00"

current time: 16:00.

In this case, method must return false.

Another example:

input time range: "10:30-12:10"

current time: 09:51.

method must return true.

Can you please give me some idea or algorithm? How I can implement this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-12-13 05:18:20

首先,您可能应该学习使用 Joda 时间

也就是说,由于时间都是用零填充的,因此您可以仅按词法比较字符串。

public static boolean inRange(String time, String range) {
  return time.compareTo(range.substring(0, 5)) >= 0
      && time.compareTo(range.substring(6)) <= 0;
}

在格式错误的输入上快速失败是一个很好的做法。

private static final Pattern VALID_TIME = Pattern.compile("[012][0-9]:[0-5][0-9]");
private static final Pattern VALID_RANGE = Pattern.compile("[012][0-9]:[0-5][0-9]-[012][0-9]:[0-5][0-9]");

然后在 inRange 的顶部放置一个断言:

assert VALID_TIME.matcher(time).matches() : time
assert VALID_RANGE.matcher(range).matches() : range

编辑:

如果您确实需要将当前时间表示为 Date,那么您应该这样比较:

 public final class Range {
   /** Inclusive as minutes since midnight */
   public final int start, end;
   public Range(int start, int end) {
     assert end >= start;
   }

   /** @param time in minutes since midnight */
   public boolean contains(int time) {
     return start <= time && time <= end;
   }

   public static Range valueOf(String s) {
     assert VALID_RANGE.matcher(s).matches() : s;
     return new Range(minutesInDay(s.substring(0, 5)),
                      minutesInDay(s.substring(6));
   }

   private static int minutesInDay(String time) {
     return Integer.valueOf(time.substring(0, 2)) * 60
         + Integer.valueOf(time.substring(3));
   }
 }

使用 < code>Range.valueOf 要从 String 进行转换,请使用您喜欢的任何日历实现将您的 Date 转换为您喜欢的任何时区午夜以来的分钟数,然后使用范围.包含

First off, you should probably just learn to use Joda time.

That said, since the times are all zero padded, you can just compare strings lexically.

public static boolean inRange(String time, String range) {
  return time.compareTo(range.substring(0, 5)) >= 0
      && time.compareTo(range.substring(6)) <= 0;
}

It's good practice to fail fast on malformed inputs.

private static final Pattern VALID_TIME = Pattern.compile("[012][0-9]:[0-5][0-9]");
private static final Pattern VALID_RANGE = Pattern.compile("[012][0-9]:[0-5][0-9]-[012][0-9]:[0-5][0-9]");

and then put an assert at the top of inRange:

assert VALID_TIME.matcher(time).matches() : time
assert VALID_RANGE.matcher(range).matches() : range

EDIT:

If you really need to represent the current time as a Date, then you should compare it this way:

 public final class Range {
   /** Inclusive as minutes since midnight */
   public final int start, end;
   public Range(int start, int end) {
     assert end >= start;
   }

   /** @param time in minutes since midnight */
   public boolean contains(int time) {
     return start <= time && time <= end;
   }

   public static Range valueOf(String s) {
     assert VALID_RANGE.matcher(s).matches() : s;
     return new Range(minutesInDay(s.substring(0, 5)),
                      minutesInDay(s.substring(6));
   }

   private static int minutesInDay(String time) {
     return Integer.valueOf(time.substring(0, 2)) * 60
         + Integer.valueOf(time.substring(3));
   }
 }

Use Range.valueOf to convert from a String, convert your Date to a number of minutes since midnight in whatever timezone you like using whatever calendar implementation you like, and then use Range.contains.

作死小能手 2024-12-13 05:18:20
Date currentDate = new Date();
Date maxDate;
Date minDate;

//Parse range to two substrings
//parse two substrings to [HH, MM]
//for HH && MM parseInt()
//
minDate= new Date.SetHour(HH); minDate.SetMinute(MM);
//repeat for max date

if(currentDate.Before(maxDate) && currentDate.After(minDate))
{
return true;
}
else
return false;
Date currentDate = new Date();
Date maxDate;
Date minDate;

//Parse range to two substrings
//parse two substrings to [HH, MM]
//for HH && MM parseInt()
//
minDate= new Date.SetHour(HH); minDate.SetMinute(MM);
//repeat for max date

if(currentDate.Before(maxDate) && currentDate.After(minDate))
{
return true;
}
else
return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文