0

From a time input in format 5:00 to 05:00!

in arr[1] there is "5:00" <--string format

 DateTimeFormatter time=DateTimeFormatter.ofPattern("HH:mm");
 LocalTime arr1 = LocalTime.parse((CharSequence) time.parse(arr[1]));
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Interesting error. You are trying to call two different `parse` methods, first `time.parse()`, then `LocalTime.parse()`. You need only one of them. I’d go for `LocalTime.parse()`. – Ole V.V. May 09 '21 at 05:53

1 Answers1

2

Works for me.

  @Test
  void localTimeFormatTest() {
    LocalTime time = LocalTime.parse("5:00", DateTimeFormatter.ofPattern("H:mm"));
    
    String result = time.format(DateTimeFormatter.ofPattern("HH:mm"));
    
    assertThat(result).isEqualTo("05:00");
  }
Ansar Ozden
  • 487
  • 6
  • 8
  • Rather than using the second formatter you also just do `time.toString()`. Could be considered dirty, though. – Ole V.V. May 09 '21 at 05:51