tl;dr
LocalTime.parse(
"9:25" ,
DateTimeFormatter.ofPattern( "H:m" )
)
.isBefore(
LocalTime.of( 10 , 0 )
)
You are using the wrong classes
Never use the terribly troublesome old date-time classes bundled with the earliest versions of Java. These are now legacy, supplanted entirely by the java.time classes built into Java 8 and later. Avoid Date
, Calendar
, and SimpleDateFormat
altogether.
Format must match input
HH:mm format
…
java.time.format.DateTimeParseException: Text '9:25' could not be parsed at index 0
Your formatting pattern says HH
which means two digit value. For hours 1-9, a leading padding zero is expected. Your input of 9:25
is missing its padding zero.
For such input, use a single H
. And likely the minutes lack a padding zero, so use a single m
there too.
DateTimeFormatter.ofPattern( "H:m" )
Do not work so hard
The java.time classes will parse your string inputs for you. No need to analyze the text yourself.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "H:m" ) ;
LocalTime lt = LocalTime.parse( "9:25" , f ) ;
Half-Open
compare a time that's between 00:00 and 09:59,
When defining a span of time, the Half-Open approach is generally the best. In this approach, the beginning is considered inclusive while the ending is exclusive. This means the hour of 9 AM starts at 09:00 and runs up to, but does not include, the first moment of the following hour, 10:00.
One benefit of Half-Open is that you avoid trying to nail down the ever-divisible last moment of the hour. Is the last moment 09:59? 09:59.9? 09:59.999999999? Avoid the entire debacle by instead asking “Is the moment before 10 AM?“.
For time-of-day only, without the context of a date, the clock stops at midnight. So if you want ask if a certain time of day is between midnight and 10 AM, you need only ask if it is before 10 AM.
LocalTime stop = LocalTime.of( 10 , 0 );
LocalTime x = LocalTime.parse( "09:25" ) ;
Boolean isEarlyEnough = x.isBefore( stop ) ;
Perhaps you want to use a starting time other than first moment of the day. In that case, specify a start as well as a stop.
LocalTime start = LocalTime.MIN ; // Constant for 00:00:00.000000000.
LocalTime stop = LocalTime.of( 10 , 0 );
LocalTime x = LocalTime.parse( "09:25" ) ;
Boolean isEarlyEnoughButNotTooEarly = ( ! x.isBefore( start ) ) && x.isBefore( stop ) ; // "Not before" is a briefer way of saying "Is equal to or later than".
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.