Java Date Format with Day Period (morning, afternoon, evening and night)

Since Java 16, we can use the additional parameter [B] to format a date-time object with the date-time information using the DateTimeFormatter class. This tutorial discusses the approaches before Java 16 as well as new formatting parameter with examples.

1. Date Format with Day Period (Since Java 16)

Starting with JDK 16+, we can go beyond AM/PM flags and format the date time strings in the following patterns via the new pattern parameter: B.

  • in the morning
  • in the afternoon
  • in the evening
  • at night

This pattern is available starting with JDK 16+ using the DateTimeFormatter and DateTimeFormatterBuilder classes.

The following program demonstrates the usage of pattern ‘B‘, representing a period of the day:

public static String formatSinceJava16(LocalDateTime ldt, ZoneId zoneId) {

  ZonedDateTime zdt = ldt.atZone(zoneId);
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MMM-dd [B]");
  return zdt.withZoneSameInstant(zoneId).format(formatter);
}

We can use this method to print the local date time (with daytime information) as follows:

LocalDateTime ldt = LocalDateTime.now();
String formattedDateTime = formatSinceJava16(ldt, ZoneId.systemDefault());
System.out.println(formattedDateTime);

The program output:

2024-Jan-21 in the afternoon

2. Date Format with Day Period (Before Java 16)

Before Java 16, if we had to create any such formatted output then we had to write our logic and append such strings in an already formatted datetime string.

LocalTime night = LocalTime.of(21, 0, 0);
LocalTime morning = LocalTime.of(6, 0, 0);
LocalTime afternoon = LocalTime.of(12, 0, 0);
LocalTime evening = LocalTime.of(18, 0, 0);
LocalTime almostMidnight = LocalTime.of(23, 59, 59);
LocalTime midnight = LocalTime.of(0, 0, 0);

public String formatBeforeJava16(LocalDateTime localDateTime, ZoneId zoneId) {

  ZonedDateTime zdt = localDateTime.atZone(zoneId);
  LocalTime lt = zdt.toLocalTime();

  DateTimeFormatter formatter
      = DateTimeFormatter.ofPattern("yyyy-MMM-dd");

  String dayTime = "";

  if ((lt.isAfter(night) && lt.isBefore(almostMidnight))
      || lt.isAfter(midnight) && (lt.isBefore(morning))) {
    dayTime = " at night";
  } else if (lt.isAfter(morning) && lt.isBefore(afternoon)) {
    dayTime = " in the morning";
  } else if (lt.isAfter(afternoon) && lt.isBefore(evening)) {
    dayTime = " in the afternoon";
  } else if (lt.isAfter(evening) && lt.isBefore(night)) {
    dayTime = " in the evening";
  }

  return zdt.withZoneSameInstant(zoneId).format(formatter) + dayTime;
}

The above code defines constants for specific times of the day, checks the argument LocalTime to determine the time of day, and sets the dayTime variable accordingly. Finally, it returns a formatted string representing the date and a description of the time of day.

LocalDateTime ldt = LocalDateTime.now();
String formattedDateTime = DayPeriodFormatter.formatBeforeJava16(ldt, ZoneId.systemDefault());
System.out.println(formattedDateTime);

The program output:

2024-Jan-21 in the afternoon

3. Summary

In this short Java tutorial, we discussed ways to format a LocalDateTime into a pattern containing the day of the time information. We discussed the declarative approach before Java 16, and then we demonstrated the use of new [B] pattern parameter with examples of each.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode