TL;DR: use the safe & concise Time.valueOf(LocalTime)
as suggested by Samuel Cruz, e.g. Time.valueOf( res.getCheckInTime() )
.
Pitfalls when using String-based conversion
The String-variant has 2 pitfalls:
LocalTime.toString()
produces variable output-formats that may cause given IllegalArgumentException
when used with Time.valueOf(String)
- two implied assumptions: (a) non-deterministic output causes (b) message-less, thus not very helpful, exception that let's the user guess which value was passed as argument and why it was illegal.
Also the JavaDoc for Time.valueOf(String)
as quoted by Jim Garrison is misleading. In current implementation (OpenJDK) you can pass any other integer-sequence separated by 2 colons, like -33:9999:88
. Although not a valid ISO-time in format HH:mm:ss
, it is apparently interpreted as time-value and may lead to serious side-effects! Did you expect "13:40:28" == Time.valueOf("-33:9999:88")
as result ?
time.LocalTime
has non-deterministic toString
format
Don't use LocalTime.toString()
because it's output format is non-deterministic and may vary in precision, worst-case omitting seconds! It tries to reduce the output to significant parts, minimum format is "HH:mm" as in JavaDoc summary.
For reliable and SQL-compliant String-format use LocalTime.format(DateTimeFormatter.ISO_LOCAL_TIME)
. It may always produce format like HH:mm:ss
as required by java.sql.Time
.
java.sql.Time
can convert LocalTime
directly
It should work when passing the argument to factory-method java.sql.Time.valueOf()
:
- either as ISO-formatted time
String("11:05:00")
(up to seconds!). ⚠️ Warning: Potential side-effects if passed String contains invalid ISO-time, e.g. "99:123:88"
or even "0:-1:-3"
(see example below).
- or directly as
java.time.LocalTime
. ✔️ Recommended as safest.
Solution: illustrates argument variants & side-effects
public static void main(String[] args) throws java.lang.Exception {
Reservation res = new Reservation("11:05", "18:05");
System.out.println(res);
System.out.printf("toString returns '11:05:00' as expected? %s\n", "11:05:00".equals(res.getCheckInTime()));
String isoTimeFormat = res.getCheckOutTime().format(DateTimeFormatter.ISO_LOCAL_TIME);
System.out.printf("explicit format() returns '%s' as expected by SQL? %s\n", isoTimeFormat, "18:05:00".equals(isoTimeFormat));
Time timeIn = Time.valueOf(res.getCheckInTime()); // recommended argument type
Time timeOut = Time.valueOf(res.getCheckOutTime().format(DateTimeFormatter.ISO_LOCAL_TIME));
Time timeTest = Time.valueOf("33:99:88"); // dangerous fun-fact
System.out.println("sql.Time in: " + timeIn + " , out: " + timeOut);
}
static class Reservation {
LocalTime checkIn;
LocalTime checkOut;
public Reservation(String in, String out) {
this.checkIn = LocalTime.parse(in);
this.checkOut = LocalTime.parse(out);
}
public LocalTime getCheckInTime() {
return checkIn;
}
public LocalTime getCheckOutTime() {
return checkOut;
}
public String toString() {
return String.format("Reservation {checkIn: %s, checkOut: %s}", getCheckInTime(), getCheckOutTime());
}
}
Try runnable demo on IDEone.
See also: