tl;dr
Duration
.between(
LocalTime.MIDNIGHT ,
myJavaSqlTime.toLocalTime()
)
.toMillis()
Avoid legacy date-time classes
Apparently you are using java.sql.Time
class. Don’t.
The date-time classes bundled with the earliest versions of Java are terrible, written by people who did not understand the nuances and complexities of date-time handling. Add on some very poor choices in design, and the result is an awful mess.
java.time
The legacy date-time classes were years ago supplanted by the modern java.time classes.
LocalTime
To represent a time-of-day, without a date and without a time zone, use LocalTime
.
LocalTime lt = LocalTime.of( 15 , 30 ) ; // 3:30 PM
LocalTime lt = LocalTime.parse( "15:30" ) ; // ditto
To get a count of milliseconds since midnight, use Duration
.
Duration d = Duration.between( LocalTime.MIDNIGHT , lt ) ;
long millis = d.toMillis() ;
Going the other direction, from a count of milliseconds since midnight to a time-of-day.
Duration d = Duration.ofMillis( 5_000 ) ;
LocalTime lt = LocalTime.MIDNIGHT.plus( d ) ;
Keep in mind that this kind of code only works for generic 24-hour days. In the real world, in various time zones, days can be other than 24 hours long. They can be 23 or 25 hours with Daylight Saving Time (DST) cutovers. And they can actually be any length as dictated by politicians, such as 23.5 hours, or off by 15 minutes, or any other adjustment deemed worthwhile for various political, cultural, diplomatic, or martial reasons.
Conversion to/from legacy class
If you must interoperate with some old code not yet updated to java.time, you can easily convert back-and-forth. Look to new methods added to the old classes. If given a java.sql.Time
object, call toLocalTime
to get a LocalTime
object.
LocalTime lt = myJavaSqlTime.toLocalTime() ;
Going the other direction, from modern class to legacy class.
java.sql.Time myJavaSqlTime = Time.valueOf( lt ) ;
Duration
If your real goal is to represent a span-of-time not related to the clock or calendar, then you should not be using LocalTime
or java.sql.Time
.
You should instead be using Duration
class to represent a span of time on the scale of hours-minutes-seconds while unattached to the timeline. (For a scale of years-month-days, use Period
class.)
To represent such a span-of-time textually, I strongly recommend not using clock-time formatting such as 00:00:05
. Such strings are inherently ambiguous, looking like a time-of-day.
ISO 8601
Instead, use standard ISO 8601 format: PnYnMnDTnHnMnS
. The P
marks the beginning. The T
separates any years-months-days from any hours-minutes-seconds. So your example of five seconds would be PT5S
.
The java.time classes use ISO 8601 formats by default when parsing/generating date-time text.
Duration d = Duration.parse( "PT5S" ) ;
Going the other direction.
String output = Duration.ofSeconds( 5 ).toString() ; // PT5S
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
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. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?