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.
Code
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
Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Indentation ConverterConvert leading indentation between tabs and spaces at a chosen width, leaving inner content untouched.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.