I have a time value stored in my database in
HH:mm:ss
time
yyyy-MM-dd HH:mm:ss
// ... Code truncated for brevity
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalTime time = resultSet.getTime("send_time").toLocalTime();
LocalDateTime datetime = time.atDate(LocalDate.now());
System.out.println(datetime.format(formatter));
Your approach is fine and should work regardless of your computer's time zone since there is no time zone information in either LocalTime
or LocalDateTime
. One possible issue is with LocalDate.now()
which returns today's date in the computer's local time zone, not in IST. You may want to replace it with LocalDate.now(ZoneId.of("Asia/Calcutta"))
.
Or as commented by @OleV.V. you could use the new driver facilities to derive a LocalTime
directly:
LocalTime time = resultSet.getObject("send_time", LocalTime.class);
Note possible caveats with your approach:
time
in mysql can store values smaller than 00:00
and larger than 23:59:59.999999
, in which case you may experience unexpected behaviours on the Java side.