java日期时间比较两个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4296949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:31:00  来源:igfitidea点击:

java date time compare two

javadate

提问by SOer

What would be the best way to compare two date in java?

在java中比较两个日期的最佳方法是什么?

Right now i have array of time like

现在我有很多时间

10.00, 12.00, 2.00, 4.00

10.00, 12.00, 2.00, 4.00

How can we find out which time to come up next compared to our computer time?

与我们的计算机时间相比,我们如何找出下一个出现的时间?

Let say right now my computer time is 3.15, what should i do to show 4.00 is next?

假设现在我的电脑时间是 3.15,接下来我该怎么做才能显示 4.00?

Date date = new Date();

采纳答案by Lachezar Balev

The java.util.Date implements the Comparable interface. That means you can very easily compare two date objects with code like this:

java.util.Date 实现了 Comparable 接口。这意味着您可以非常轻松地使用以下代码比较两个日期对象:

if (date1.compareTo(date2) < 0)
{
        System.out.println("date1 is before date2");
}
else if (date1.compareTo(date2) > 0)
{
    System.out.println("date1 is after date2");
}
else
{
    System.out.println("date1 is equal to date2");
}

回答by Jon Skeet

I would use Joda Time. Create a LocalTimefor each of the times you're interested in, and just new LocalTime()to get the current time of day... then you can compare them either with compareToor isBeforeand isAfter.

我会使用Joda TimeLocalTime为您感兴趣的每个时间创建一个,只是new LocalTime()为了获取当前时间...然后您可以将它们与compareToisBefore和进行比较isAfter

Using Joda Time instead of the built-in Date/Calendarclasses will give you much more readable code for this sort of thing.

使用 Joda Time 而不是内置的Date/Calendar类将为此类事情提供更具可读性的代码。

回答by Kennet

JodaTime seems to be the standard answer for questions like these, but I haven't had the time (no pun intended) yet to check out Joda, so here is my Calendar suggestion:

JodaTime 似乎是此类问题的标准答案,但我还没有时间(没有双关语意)查看 Joda,所以这是我的日历建议:

public static String getNextHour() {
    Calendar c = new GregorianCalendar();
    int minsLeft = 60 - c.get(Calendar.MINUTE);
    c.add(Calendar.MINUTE, minsLeft);

    SimpleDateFormat sdf = new SimpleDateFormat("h:mm");
    return sdf.format(c.getTime());
}

回答by Basil Bourque

The java.util.Dateclass is the wrong type for your needs. That class represents a date plusa time-of-day, and you want only a time-of-day. Amongst the old date-time classes bundled with the early versions of Java there is noclass to truly represent a time-of-day value. The java.sql.Timeclass pretends to do so but is a hack.

java.util.Date班是错误的类型满足您的需求。该类表示一个日期加上一个时间,而您只需要一个时间。在与 Java 早期版本捆绑在一起的旧日期时间类中,没有任何类可以真正表示时间值。该java.sql.Time班假装这样做不过是一个黑客。

java.time

时间

The java.timeframework is built into Java 8 and later. See Oracle Tutorial. These classes were inspired by the highly successful Joda-Timelibrary. Much of the java.time functionality is back-ported to Jave 6 & 7and further adapted to Android. These classes supplant the old date-time classes; a vastimprovement.

java.time框架是建立在Java 8和更高版本。请参阅Oracle 教程。这些类的灵感来自非常成功的Joda-Time库。许多 java.time 功能向后移植到 Jave 6 & 7并进一步适应 Android。这些类取代了旧的日期时间类;一个巨大的改进。

The LocalTimeclass represents a time-of-day without a date and without a time zone.

LocalTime级表示没有日期,没有一个时区时间的一天。

Use a sorted collection such as a Listto organize your values.

使用诸如 a 之类的排序集合List来组织您的值。

List<LocalTime> times = new ArrayList<>( 4 );
times.add( LocalTime.of( 10 , 0 );
times.add( LocalTime.of( 12 , 0 );
times.add( LocalTime.of( 14 , 0 );
times.add( LocalTime.of( 16 , 0 );

Determine your sample time for comparison.

确定您的采样时间以进行比较。

LocalTime sample = LocalTime.of( 15 , 15 ); // Quarter-hour past 3 in the afternoon.

Or get the current time-of-day. Specify a time zoneas that is more reliable than implicitly depending on your JVM's current default time zone.

或者获取当前的时间。指定一个时区,因为它比隐式更可靠,具体取决于 JVM 的当前默认时区

ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalTime sample = LocalTime.now( zoneId );

Loop the Listto find the first item that is later in the day than your sample time. To compare, use the isAfter, isBefore, or equalsmethods.

循环List查找当天晚于您的采样时间的第一项。为了比较,使用isAfterisBeforeequals方法。

LocalTime hit = null;
for ( LocalTime time : times ) {
    if ( time.isAfter( sample ) ) {
        hit = time;
        break; // Bail-out of this FOR loop.
    }
}
// Test if 'hit' is still null, meaning no appropriate value found.
if ( null == hit ) { … } else ( … }