1

I have a time in the format HHMM (1643) and I want it to be formatted to HH:MM AM/PM (04:43 PM)

How can I achieve this in Java?

I'm doing something similar for the date which is formatted YYYMMDD (20201013) into MM/DD/YYY (10/13/2020) with this code:

new java.text.SimpleDateFormat("yyyyMMdd").parse($F{date})
Eklavya
  • 17,618
  • 4
  • 28
  • 57
Jacob Peterson
  • 343
  • 1
  • 4
  • 12

1 Answers1

4

You can use DateTimeFormatter & LocalTime from java.time

First, parse the time as LocalTime using DateTimeFormatter. Then again format the LocalTime into String in your desire format.

LocalTime time = LocalTime.parse("1643", DateTimeFormatter.ofPattern("HHmm"));
String formattedTime = time.format(DateTimeFormatter.ofPattern("hh:mm a"));
System.err.println(formattedTime);

Output: 04:43 PM

Eklavya
  • 17,618
  • 4
  • 28
  • 57