Formatting a Apex Time into String (in apex class)

Written by Vikash AgarwalJune 6th, 2022
Formatting a Apex Time into String (in apex class) Formatting a Apex Time into String (in apex class)

Since as on today we don’t have any inbuilt method which can format this type of String “ 14:00:00.000Z” like “2:00 PM” So wrote a snippet for this and may be useful.

1/**
2   * Expecting a time string will be looks like  14:00:00.000Z and expected output
3   * will be 02:00 PM/AM
4   */
5  public static String getFormattedTime(String sTime) {
6    if (String.isNotBlank(sTime)) {
7      List<String> listOfTimeToken = sTime.split(':');
8      if (
9        listOfTimeToken.size() == 3 &&
10        String.isNotBlank(listOfTimeToken[0]) &&
11        String.isNotBlank(listOfTimeToken[1])
12      ) {
13        Integer hr = Integer.valueOf(listOfTimeToken[0]);
14        String min = listOfTimeToken[1];
15String aMPM = hr > 12 ? ' PM' : ' AM';
16        hr = Math.mod(hr, 12);
17        hr = hr == 0 ? 12 : hr;
18        String sHr = hr.format();
19        sHr = sHr.leftPad(2, '0');
20        sTime = sHr + ':' + min + aMPM;
21      }
22    }
23    return sTime;
24  }