简体   繁体   English

java日期时间比较两个

[英]java date time compare two

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();

The java.util.Date implements the Comparable interface. java.util.Date实现Comparable接口。 That means you can very easily compare two date objects with code like this: 这意味着您可以非常轻松地将两个日期对象与以下代码进行比较:

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");
}

I would use Joda Time . 我会用Joda Time Create a LocalTime for 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 compareTo or isBefore and isAfter . 为您感兴趣的每个时间创建一个LocalTime ,只需要new LocalTime()来获取当前时间...然后您可以将它们与compareToisBeforeisAfter

Using Joda Time instead of the built-in Date / Calendar classes will give you much more readable code for this sort of thing. 使用Joda Time而不是内置的Date / Calendar类将为您提供更易读的代码。

The java.util.Date class is the wrong type for your needs. java.util.Date类是您需要的错误类型。 That class represents a date plus a 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 no class to truly represent a time-of-day value. 在与早期版本的Java捆绑在一起的旧日期时间类中, 没有一个类可以真正表示时间值。 The java.sql.Time class pretends to do so but is a hack. java.sql.Time类假装这样做但是是一个hack。

java.time java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 See Oracle Tutorial . 请参阅Oracle教程 These classes were inspired by the highly successful Joda-Time library. 这些课程的灵感来自非常成功的Joda-Time图书馆。 Much of the java.time functionality is back-ported to Jave 6 & 7 and further adapted to Android . 大部分java.time功能都被后端移植到Jave 6和7,并进一步适应了Android These classes supplant the old date-time classes; 这些类取代了旧的日期时间类; a vast improvement. 一个巨大的进步。

The LocalTime class represents a time-of-day without a date and without a time zone. LocalTime类表示没有日期且没有时区的时间。

Use a sorted collection such as a List to organize your values. 使用排序的集合(如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 zone as 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 List to find the first item that is later in the day than your sample time. 循环List以查找当天晚些时候的第一个项目而不是您的采样时间。 To compare, use the isAfter , isBefore , or equals methods. 要进行比较,请使用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 ( … }

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());
}

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

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