简体   繁体   English

您如何比较 Java 中的时间?

[英]How do you compare times in Java?

I'm wondering how I would compare times in a Java program.我想知道如何比较 Java 程序中的时间。 For example: If I choose a time block from 09:00-12:00 and set that.例如:如果我从 09:00-12:00 选择一个时间段并设置它。 If i choose a second time and choose 11:00-13:00 time period, I would want to be able to have an error pop up, but I'm not exactly sure how to set that up.如果我选择第二次并选择 11:00-13:00 时间段,我希望能够弹出一个错误,但我不确定如何设置它。

For example if you are reserving a time at a massage therapy place from 9-12, you can't reserve another one for yourself at 11-1, it wouldn't make sense.例如,如果您在 9-12 点预约按摩理疗场所的时间,您不能在 11-1 点为自己预约另一个时间,这是没有意义的。 I'm trying to figure out how to put it into code.我试图弄清楚如何将其放入代码中。 My noob level is having me think to have a string as a selection method, then parse the string to integer and compare the integer for overlapping?我的菜鸟级别让我认为将字符串作为选择方法,然后将字符串解析为 integer 并比较 integer 是否重叠? I want to use the Date class though because it seems very useful and I'd like to learn how to use it.我想使用Date class 因为它看起来非常有用,我想学习如何使用它。

Let's say you have limiting times in which you must create a time block for whatever reason.假设您有时间限制,无论出于何种原因,您都必须在其中创建一个时间块。 Let's convert those limiting times into actual Date objects in Java.让我们将这些限制时间转换为 Java 中的实际 Date 对象。 I am using我在用

Date today = new Date();

to get today's date, you can use whatever day you want.要获得今天的日期,您可以使用任何您想要的日期。
I then merge today's date (day of month of year) with the times I defined in two strings, but which you could get from an object, etc.然后,我将今天的日期(一年中的某天)与我在两个字符串中定义的时间合并,但您可以从 object 等中获得。

private static Date combineDates(Date date, Date time) {
    Calendar cal = Calendar.getInstance();

    cal.setTime(time);
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int min = cal.get(Calendar.MINUTE);

    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, min);

    return cal.getTime();
}


Date today = new Date();
SimpleDateFormat isoTime = new SimpleDateFormat("HH:mm", Locale.US);

String startTimeString =  "09:00";
String endTimeString =  "12:00";

Date startTime = new Date();
Date endTime = new Date();

try {
    startTime = isoTime.parse(startTimeString);
    endTime = isoTime.parse(endTimeString);
} catch (ParseException e){
    Log.e(TAG, e.getMessage());
}

Date startDate = combineDates(today, startTime);
Date endDate = combineDates(today, endTime);

From then on the only thing left to do is to get the second chosen time period and comparing it to the limiting times.从那时起,唯一剩下要做的就是获得第二个选择的时间段并将其与限制时间进行比较。

Something like:就像是:

if (newDateStart.after(startDate) && newDateEnd.before(endDate)) {
// Allow user to choose time
} else {
// Do not allow user to choose time
}

I am sure it is a bit convoluted but it's the best I've got right now.我敢肯定这有点令人费解,但这是我目前所拥有的最好的。 Also note that there are newer Java objects you can use but this will work with most Android APIs so...另请注意,您可以使用更新的 Java 对象,但这将适用于大多数 Android API,所以...

LocalTime from java.time本地时间来自 java.time

    LocalTime blockStart = LocalTime.of(9, 0);
    LocalTime blockEnd = LocalTime.of(12, 0);
    assert blockEnd.isAfter(blockStart);

    LocalTime newBlockStart = LocalTime.of(11, 0);
    LocalTime newBlockEnd = LocalTime.of(13, 0);
    assert newBlockEnd.isAfter(newBlockStart);

    if (newBlockStart.isBefore(blockEnd) && newBlockEnd.isAfter(blockStart)) {
        System.out.println("" + newBlockStart + '–' + newBlockEnd
                + " overlaps with " + blockStart + '–' + blockEnd);
    }

Output from the snippet is:片段中的 Output 是:

11:00–13:00 overlaps with 09:00–12:00 11:00–13:00 与 09:00–12:00 重叠

A LocalTime is a time of day without a date. LocalTime是没有日期的一天中的时间。 A potential liability is that it cannot take into account if the clock is turned forward or backward, for example when summer time (DST) begins or ends.一个潜在的责任是它无法考虑时钟是向前还是向后转动,例如夏令时 (DST) 何时开始或结束。 But if you want nothing that fancy, it's the correct class to use.但是,如果您不想要任何花哨的东西,那就是正确的 class 使用。

The formula for detecting an overlap is standard.检测重叠的公式是标准的。 I include a link to a question about it below.我在下面包含了一个关于它的问题的链接。

I want to use the Date class though…我想使用Date class 虽然......

Don't, don't, please don't.不要,不要,请不要。 The Date is poorly designed and long outdated. Date设计不佳且早已过时。 You are seriously better off not learning how to use that class.你最好不要学习如何使用 class。 Learn to use java.time, the modern Java date and time API.学习使用 java.time,现代 Java 日期和时间 API。 A possible place to start is the first link below.一个可能的起点是下面的第一个链接。

Links链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM