How to parse HH:mm:ss without hours?

31
May 16, 2019, at 7:10 PM

I have a string with format like: "2h 33m 50s". If hours reach zero this string changes to: "33m 50s". So, if this string goes through LocalTime.parse it throw exception. How can I parse it?

fun main() {
    val timeString = "2h 33m 50s"
    val timeString2 = "33m 50s"
    val formatterH = DateTimeFormatter.ofPattern("[H]'h 'm[m]'m 's[s]'s'")
    val formatterM = DateTimeFormatter.ofPattern("m[m]'m 's[s]'s'")
    val time = LocalTime.parse(timeString, formatterH)
    println(time)
    val time2 = LocalTime.parse(timeString2, formatterH) //throws exception
    println(time2)
    val time3 = LocalTime.parse(timeString2, formatterM) //throws similar exception
    println(time3)
}
Program output:
02:33:50
Exception in thread "main" org.threeten.bp.format.DateTimeParseException: Text '33m 50s' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: DateTimeBuilder[fields={SecondOfMinute=50, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=33, MilliOfSecond=0}, ISO, null, null, null], type org.threeten.bp.format.DateTimeBuilder
    at org.threeten.bp.format.DateTimeFormatter.createError(DateTimeFormatter.java:1559)
    at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1496)
    at org.threeten.bp.LocalTime.parse(LocalTime.java:437)
    at MainKt.main(main.kt:16)
    at MainKt.main(main.kt)
Caused by: org.threeten.bp.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: DateTimeBuilder[fields={SecondOfMinute=50, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=33, MilliOfSecond=0}, ISO, null, null, null], type org.threeten.bp.format.DateTimeBuilder
    at org.threeten.bp.LocalTime.from(LocalTime.java:405)
    at org.threeten.bp.LocalTime$1.queryFrom(LocalTime.java:116)
    at org.threeten.bp.LocalTime$1.queryFrom(LocalTime.java:113)
    at org.threeten.bp.format.DateTimeBuilder.build(DateTimeBuilder.java:642)
    at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1492)
Answer 1
tl;dr

You are working too hard, and going in the wrong direction.

Regex is overkill for this problem.

No need for DateTimeFormatter class, nor a formatting pattern.

Use Duration class to parse a ISO 8601 string crafted from your input.

Duration                    // Represent a span-of-time not attached to the timeline with class `Duration`, not `LocalTime`.
.parse(                     // By default, the *java.time* classes such as `Duration` use the standard ISO 8601 formats to parse/generate date-time strings.
    "PT" + "2h 33m 50s"     // Morph your input string to comply with the ISO 8601 standard. Add `P` for the beginning, and `T` to separate years-months-days from hours-minutes-seconds. 
    .replace( " " , "" )    // Delete any SPACE characters by replacing them with nothing.
    .toUpperCase()          // Force all the letters to be uppercase.
)                           // Returns a `Duration`.

Ditto for just minutes and seconds.

Duration.parse( "PT" + "33m 50s".replace( " " , "" ).toUpperCase() ) 
Duration, not LocalTime

if this string goes through LocalTime.parse

LocalTime is for a time-of-day. Your input is not a time-of-day.

Your input string represents a span-of-time unattached to the timeline. The class for that is Duration.

ISO 8601

Your input string is close to standard ISO 8601 format, PnYnMnDTnHnMnS. The P marks the beginning. The T separates any years-months-days from any hours-minutes-seconds.

Let's adjust your input string to comply with the standard.

String input = "PT" + "2h 33m 50s".replace( " " , "" ).toUpperCase() ;

input: PT2H33M50S

Parse.

Duration d = Duration.parse( input ) ;  // PT2H33M50S

To generate a string in standard ISO 8601, call toString.

String output = d.toString() ;

output: PT2H33M50S

You can add that Duration object to a time-of-day, a LocalTime.

LocalTime lt = LocalTime.NOON.plus( d ) ;

You can add that Duration to the current moment in UTC to determine a future moment (or a past moment is the duration is a negative amount).

Instant instant = Instant.now().plus( d ) ;

lt.toString(): 14:33:50

See all the above code run live at IdeOne.com.

You can extract each part of the Duration.

long daysPart = d.toDaysPart() ;  // 24-hour chunks of time, not related to calendar days.
int hoursPart = d.toHoursPart() ;
int minutesPart = d.toMinutesPart() ;
int secondsPart = d.toSecondsPart() ;

Or perhaps you want the entire span-of-time as a count of total milliseconds.

long millis = d.toMillis() ;  // All the hours-minutes-seconds and such totaled as one count of elapsed milliseconds.
Answer 2

Use pattern "[H'h ']m'm 's's'", which will parse both.

  • [H'h '] matches the optional hours, e.g. "2h "
  • m'm ' matches "33m "
  • s's' matches "50s"
READ ALSO
Is JDK 8’s new strategy for dealing with HashMap collisions(tree instead of list) need only for those who is not able to write a good hashcode()?

Is JDK 8’s new strategy for dealing with HashMap collisions(tree instead of list) need only for those who is not able to write a good hashcode()?

In Java 8, HashMap replaces linked list with a binary tree when the number of elements in a bucket reaches certain threshold

45
How to disable Jackson from deserializing an Instant from epoch millis?

How to disable Jackson from deserializing an Instant from epoch millis?

I am developing an API using Spring Boot and am using Jackson for payload (de)serializationI want to deserialize an ISO-8601 formatted datetime into a java

17
what does it mean BuildConfig.FLAVOR in this code

what does it mean BuildConfig.FLAVOR in this code

I need some Help about BuildConfigFLAVOR

24