Day Period Pattern B Formats Morning

Pattern letter B formats the finer day period — morning, afternoon, evening, night — where am/pm collapses the whole day into two halves.

JavaCli Tooling

Code

snippet
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DayPeriodFormat {

    public static void main(String[] args) {
        DateTimeFormatter dayPeriod =
                DateTimeFormatter.ofPattern("h B", Locale.US); // B formats the finer day period than am/pm

        LocalDateTime morning = LocalDateTime.of(2026, 11, 21, 9, 15);
        LocalDateTime night = LocalDateTime.of(2026, 11, 21, 23, 45);

        System.out.println("morning: " + morning.format(dayPeriod));
        System.out.println("night:   " + night.format(dayPeriod));

        // 对照:a 只有 am/pm 两档,B 能区分 morning / afternoon / evening / night
        DateTimeFormatter amPm = DateTimeFormatter.ofPattern("h a", Locale.US);
        System.out.println("am/pm:   " + night.format(amPm));
    }
}

Under the hood

The snippet is organized around the class DayPeriodFormat and the function main.

What to watch for

  • Any imports involved are standard-library modules.

Applying it

The main signature reads public static void main(String[] args)

Look for labeled output — morning:, night:, am/pm: — when the file runs.

Statements that matter

DateTimeFormatter.ofPattern("h B", Locale.US); // B formats the finer day period than am/pm
LocalDateTime morning = LocalDateTime.of(2026, 11, 21, 9, 15);
DateTimeFormatter amPm = DateTimeFormatter.ofPattern("h a", Locale.US);
LocalDateTime night = LocalDateTime.of(2026, 11, 21, 23, 45);

Interactive Tools for This Topic

FAQ

What does Day Period Pattern B Formats Morning print when run?
Run it as a complete program — the output lines carry labels such as `morning:`, `night:`, `am/pm:`.