简体   繁体   English

如何比较 Java 8 中 Date 对象之间的时间?

[英]How can I compare time of the day between Date objects in Java 8?

Given two Date objects that include hours and minutes, how can I elegantly find out which one of them has the earliest time of the day?给定两个包含小时和分钟的Date对象,我如何优雅地找出其中哪一个具有一天中最早的时间?

Eg if date1 is 2018-01-01 at 09:00 , and date2 is 2018-01-02 at 08:00 , then per this specification, date2 < date1 .例如,如果date12018-01-01 at 09:00 ,而date22018-01-02 at 08:00 ,那么根据本规范, date2 < date1

You can use the compareTo -Method of LocalTime if you convert the Date before.如果您之前转换DateLocalTime可以使用 LocalTime 的compareTo -Method。

Convert like this (found at https://www.baeldung.com/java-date-to-localdate-and-localdatetime ):像这样转换(在https://www.baeldung.com/java-date-to-localdate-and-localdatetime找到):

public static LocalTime convertToLocalTimeViaInstant(Date dateToConvert) {
    return dateToConvert.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalTime();
}

and compare like this:并像这样比较:

time1.compareTo(time2);

If u want to use a method u can use the conversion like this:如果你想使用一种方法,你可以使用这样的转换:

public static int compareTimeOfDates(Date date1, Date date2) {
    return convertToLocalTimeViaInstant(date1).compareTo(convertToLocalTimeViaInstant(date2));
}

You can try to do something like:您可以尝试执行以下操作:

boolean compareTime(Date date1, Date date2) {
    DateFormat df = new SimpleDateFormat("HHmm");
    int timeDate1 = Integer.valueOf(df.format(date1));
    int timeDate2 = Integer.valueOf(df.format(date2));
    return timeDate1 >= timeDate2;
}

Edit 2:编辑2:

//just a sample. base on your case, set the date you have to here.
        Date date1 = Calendar.getInstance().getTime(); 
        Date date2 = Calendar.getInstance().getTime();

        LocalDateTime lcdate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime lcdate2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();


        int date1Hour = lcdate1.getHour();
        int date1Minute = lcdate1.getMinute();

        int date2Hour = lcdate2.getHour();
        int date2Minute = lcdate2.getMinute();

More detail is here: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html更多细节在这里: https : //docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

Hope this help.希望这有帮助。

int compareTimeOfDate(Date a, Date b) {
    LocalDateTime localA = a.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    int hourA = localA.getHour();
    int minuteA = localA.getMinute();

    LocalDateTime localB = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    int hourB = localB.getHour();
    int minuteB = localB.getMinute();

    if (hourA == hourB && minuteA == minuteB) {
        return 0;
    }

    if (hour1 < hour2 || hour1 == hour2 && minute1 < minute2) {
        return -1;
    }

    return 1;
}

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

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