0

Essentially I'm going to be reading in a time string from the user in the format "HH:MM:SS" in 24hr format. I want to be able to say if HH > 12, then assign a variable "PM" and if HH < 12, then assign a variable "AM".

Is splitting the string and then parsing the HH portion of that string the most efficient way to extract that segment for my purposes?

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
fuzzdelay
  • 47
  • 5
  • It would be slightly more efficient to convert the first one or two characters to char, and then from char to int, before you test if the int id is greater than 12. – Gilbert Le Blanc Mar 21 '20 at 20:57
  • No need to access the hour part directly. Use like `LocalTime.parse("23:56:45").format(DateTimeFormatter.ofPattern("a", Locale.ENGLISH))`. This example yields `"PM"`. – Ole V.V. Mar 22 '20 at 04:50

0 Answers0