I have a string that represents time duration of 54:34:41 i.e. 54 hours, 34 minutes, 41 seconds.
I would like to extract the 54 hours and subtract it from the current system time.
However when I run below I get java.time.format.DateTimeParseException: Text '54:34:41' could not be parsed: Invalid value for HourOfDay (valid values 0 - 23): 54
How can I extract 54 hours and subtract from current time?
private val formatterForTime: DateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss") val timeDuration = formatterForTime.parse("54:34:41") val currentTime = LocalDateTime.now() val newTime = currentTime.minusHours(timeDuration.get(ChronoField.HOUR_OF_DAY).toLong())
LocalDateTime.nowis the right thing to do. That type lacks the context of a time zone or offset from UTC. So it cannot represent a moment, a specific point on the timeline.splitfunction and instead pull index[1](minute) and[2](seconds), then usezdt.minusMinutesandzdt.minusSecondsrespectively