比较时间:检查给定时间是否在另一个给定的时间内。

时间:2021-04-27 21:34:13

As the title states; Is there a way to compare times to see if they are within a certain amount of time of each other? Ie. a way to tell if Time1 is within 15 minutes of Time2. A more complicated example might be within 75 minutes (because the hour will have changed as well, therefore resetting the minute count to 0.

正如书名;有没有一种方法可以比较时间,看看它们是否在一定的时间内?Ie。一种方法来判断Time1是否在Time2的15分钟内。一个更复杂的示例可能在75分钟内(因为时间也会改变,因此将分钟数重置为0)。

There are lots of different ways of comparing dates and times in java/android. There is the Time class, Date class and Calendar class. There's even a compareTo method and a method to check if a date is before or after the other, but I can't find a solution to my problem.

在java/android中有很多不同的方法来比较日期和时间。有时间类、日期类和日历类。甚至有一个比较方法和方法来检查日期是否在另一个日期之前或之后,但我找不到解决我问题的方法。

Another example:

另一个例子:

If I have a Time1, 12:30, and Time2 , 12:10, and I want to check if they are withing 15 minutes of each other. I can compare the difference of the difference of the minutes. 30 - 10 = 20. 20 is clearly not less than 15.

如果我有一个时间,12:30,和Time2, 12:10,我想检查他们是否在15分钟之内。我可以比较一下分钟的差值。30 - 10 = 20。20显然不小于15。

However, is Time1 was 12:56 and Time2 was 13:02, this method wouldn't work. 2 - 56 = -54, but the actual difference is 6. One solution is that if you know which time is later, check if the later minute is less greater than the earlier minute. If it isn't, then add 60 to the earlier minute.

但是,Time1是12:56,Time2是13:02,这个方法行不通。2 - 56 = -54,但实际差别是6。一种解决方法是,如果你知道哪一种时间比较晚,那就检查下一分钟是否比前一分钟更短。如果不是,则在前一分钟增加60。

Is this a good method of comparing? This will add lots of complexity to my code, especially as I need to check the hours and date as well. Is there a simpler solution or api that is available?

这是比较的好方法吗?这将增加我的代码的复杂性,特别是我需要检查工时和日期。有更简单的解决方案或api吗?

4 个解决方案

#1


5  

Use the timestamp (milliseconds since 1970) to check the difference of the dates.

使用时间戳(自1970年以来的毫秒)来检查日期的不同。

long timestamp1 = date1.getTime();
long timestamp2 = date2.getTime();
if (Math.abs(timestamp1 - timestamp2) < TimeUnit.MINUTES.toMillis(15)) {
    …
}

#2


0  

The best way to compare times is to covert everything to one standard unit. I would write a simple method to take the hours and multiply them by 60 to convert them to minutes. Then since you have the time in minutes the comparison is easy.

比较时间的最好方法是把所有的东西都转换成一个标准单位。我要写一个简单的方法,花几个小时把它们乘以60,把它们转换成分钟。然后,因为你有时间在几分钟内,比较是容易的。

#3


0  

Try this if you only have have to compare the format hh:mm:

如果你只需要比较一下格式hh:mm:

String txtTime1; // input with format hh:mm
String txtTime2; // input with format hh:mm
int hour1 = Integer.parseInt(txtTime1.split(":")[0]);
int hour2 = Integer.parseInt(txtTime2.split(":")[0]);
int min1  = Integer.parseInt(txtTime1.split(":")[1]);
int min2  = Integer.parseInt(txtTime2.split(":")[1]);
int time1 = hour1 * 60 + min1;
int time2 = hour2 * 60 + min2;
if(Math.abs(time1 - time2) <= 15) {
    // equal or less then 15 minute difference
} else {
    // more then 15 minute difference
}

If you have the compare entire dates I suggest you use the UNIX timestamp method.

如果您有比较完整的日期,我建议您使用UNIX时间戳方法。

#4


0  

Try this :

试试这个:

Calendar start_time = Calendar.getInstance();
// set exact start_time here
Calendar end_time = Calendar.getInstance();
// set exact end_time here

// Make sure to set Calendar.MINUTE
start_time.add(Calendar.MINUTE, 15); // adding 15 minutes to the start_time
if (start_time.after(end_time)) {
    // times are less than or equal to 15 mins apart
} else {
    // times are more than 15 mins apart
}

#1


5  

Use the timestamp (milliseconds since 1970) to check the difference of the dates.

使用时间戳(自1970年以来的毫秒)来检查日期的不同。

long timestamp1 = date1.getTime();
long timestamp2 = date2.getTime();
if (Math.abs(timestamp1 - timestamp2) < TimeUnit.MINUTES.toMillis(15)) {
    …
}

#2


0  

The best way to compare times is to covert everything to one standard unit. I would write a simple method to take the hours and multiply them by 60 to convert them to minutes. Then since you have the time in minutes the comparison is easy.

比较时间的最好方法是把所有的东西都转换成一个标准单位。我要写一个简单的方法,花几个小时把它们乘以60,把它们转换成分钟。然后,因为你有时间在几分钟内,比较是容易的。

#3


0  

Try this if you only have have to compare the format hh:mm:

如果你只需要比较一下格式hh:mm:

String txtTime1; // input with format hh:mm
String txtTime2; // input with format hh:mm
int hour1 = Integer.parseInt(txtTime1.split(":")[0]);
int hour2 = Integer.parseInt(txtTime2.split(":")[0]);
int min1  = Integer.parseInt(txtTime1.split(":")[1]);
int min2  = Integer.parseInt(txtTime2.split(":")[1]);
int time1 = hour1 * 60 + min1;
int time2 = hour2 * 60 + min2;
if(Math.abs(time1 - time2) <= 15) {
    // equal or less then 15 minute difference
} else {
    // more then 15 minute difference
}

If you have the compare entire dates I suggest you use the UNIX timestamp method.

如果您有比较完整的日期,我建议您使用UNIX时间戳方法。

#4


0  

Try this :

试试这个:

Calendar start_time = Calendar.getInstance();
// set exact start_time here
Calendar end_time = Calendar.getInstance();
// set exact end_time here

// Make sure to set Calendar.MINUTE
start_time.add(Calendar.MINUTE, 15); // adding 15 minutes to the start_time
if (start_time.after(end_time)) {
    // times are less than or equal to 15 mins apart
} else {
    // times are more than 15 mins apart
}