JAVA检查时间范围。怎样
例如,我输入参数的格式为:04:00-06:00或23:00-24:00。参数类型-字符串 在我的方法中,我必须检查,输入参数中的时间范围不在当前时间之前。我怎么能做到 更多详情: 输入时间范围:12:00-15:00 当前时间:16:00 在这种情况下,方法必须返回false 另一个例子: 输入时间范围:10:30-12:10 当前时间:09:51 方法必须返回trueJAVA检查时间范围。怎样,java,date,time,date-range,Java,Date,Time,Date Range,例如,我输入参数的格式为:04:00-06:00或23:00-24:00。参数类型-字符串 在我的方法中,我必须检查,输入参数中的时间范围不在当前时间之前。我怎么能做到 更多详情: 输入时间范围:12:00-15:00 当前时间:16:00 在这种情况下,方法必须返回false 另一个例子: 输入时间范围:10:30-12:10 当前时间:09:51 方法必须返回true 你能给我一些想法或算法吗?如何实现此方法?首先,您应该学会使用 这就是说,由于时间都是零填充的,所以您可以只按词汇比较字符串
你能给我一些想法或算法吗?如何实现此方法?首先,您应该学会使用 这就是说,由于时间都是零填充的,所以您可以只按词汇比较字符串
public static boolean inRange(String time, String range) {
return time.compareTo(range.substring(0, 5)) >= 0
&& time.compareTo(range.substring(6)) <= 0;
}
然后在inRange的顶部放置一个断言:
编辑:
如果确实需要将当前时间表示为日期,则应通过以下方式进行比较:
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));
}
}
使用Range.valueOf要从字符串转换,请使用您喜欢的日历实现将日期转换为从午夜开始的任意时区的分钟数,然后使用Range.contains。首先,您可能应该学会使用 这就是说,由于时间都是零填充的,所以您可以只按词汇比较字符串
public static boolean inRange(String time, String range) {
return time.compareTo(range.substring(0, 5)) >= 0
&& time.compareTo(range.substring(6)) <= 0;
}
然后在inRange的顶部放置一个断言:
编辑:
如果确实需要将当前时间表示为日期,则应通过以下方式进行比较:
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));
}
}
使用Range.valueOf要从字符串转换,请使用您喜欢的日历实现,将您的日期转换为从午夜开始的任意时区的分钟数,然后使用Range.contains。当前时间是日期,而不是字符串。我同意您的看法。这是一个很好的解决方案。。。但是我不能使用任何图书馆。只有标准的功能和标准的软件包。@user471011,你到底为什么不能使用库呢?什么是标准功能?上面的Range类对你不适用吗?当前时间是Date,但不是String。我同意你的看法。这是一个很好的解决方案。。。但是我不能使用任何图书馆。只有标准的功能和标准的软件包。@user471011,你到底为什么不能使用库呢?什么是标准功能?上面的Range类对你不适用吗?这应该被标记为家庭作业吗?不,这不是家庭作业。你错了。这应该标记为家庭作业吗?不。这不是家庭作业。你错了。setHours和setMinute是不推荐使用的方法。很抱歉,我在阅读旧文档时使用了Calendar类中的等效方法。e、 g.maxDate=新日历;maxDate.setHOUR,HH;currentDate.aftermaxDate;setHours和setMinute是不推荐使用的方法。很抱歉,我在阅读旧文档时使用了Calendar类中的等效方法。e、 g.maxDate=新日历;maxDate.setHOUR,HH;currentDate.aftermaxDate;
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;