Java16 introduced JDK-8247781 Day periods support to
java.time.format.DateTimeFormatter/DateTimeFormatterBuilder class

Before Java16, It supports to display the Time such as 2AM or 11PM

Following example uses Current time using DateTimeFormatter with option a.

Option a is an alias for am-pm-of-day, prints AM or PM based on Time. DateTimeFormatter option h for display an hour

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalTime date = LocalTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h a");
        var result=date.format(formatter);
        System.out.println(result);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("h a");
        var result1=date.format(formatter);
        System.out.println(result1);
    }
}

Java16 introduced below Pattern letters formatting options for day period. It allows you to print 9 in the morning instead of 9 AM

  • B - Print the Day period in SHORT such as in the afternoon, in the morning,and at night
  • BBBB - in FULL format
  • BBBBB -in NARROW format

Here is an example

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalTime time = LocalTime.now();
        System.out.println(time);  //09:41:03.705705600
        
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("h B");
        var result=time.format(formatter);
        System.out.println(result); // 9 in the morning

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("h BBBB");
        var result1=time.format(formatter);
        System.out.println(result1); // 9 in the morning
    }
}

The same day period pattern letters applies to DateTimeFormatterBuilder class in java.time.format package