java.util.Date . , . , Java, , . java.sql.Time , , .
java.time
java.time Java 8 . . Oracle. rel= no nofollow. > Joda-Time. java.time back-ported Jave 6 7 Android. ; .
LocalTime .
, 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 );
.
LocalTime sample = LocalTime.of( 15 , 15 ); // Quarter-hour past 3 in the afternoon.
Or get the current time of day. Specify the time zone as it is more reliable than implicit, depending on your default JVM current time zone .
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalTime sample = LocalTime.now( zoneId );
Scroll Listto find the first item that will be later in the day than your sample time. For comparison, use isAfter, isBeforeor equals.
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 ( … }
source
share