What would be the best way to compare two date in java?
Right now i have array of time like
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?
Date da开发者_如何学编程te = new Date();
The java.util.Date implements the Comparable interface. 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. 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
.
Using Joda Time instead of the built-in Date
/Calendar
classes will give you much more readable code for this sort of thing.
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:
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());
}
The java.util.Date
class is the wrong type for your needs. 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. The java.sql.Time
class pretends to do so but is a hack.
java.time
The java.time framework is built into Java 8 and later. See Oracle Tutorial. These classes were inspired by the highly successful Joda-Time library. Much of the java.time functionality is back-ported to Jave 6 & 7 and further adapted to 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.
Use a sorted collection such as a List
to organize your values.
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.
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. To compare, use the isAfter
, isBefore
, or equals
methods.
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 ( … }
精彩评论