An interesting question. Joda-Time and java.time (JSR-310) have different design centres - Joda-Time is an open source library, but java.time is an extension to the JDK. One practical implication is more pressure on method count, which was a factor here.
The method in question in Joda-Time is one of four:
toString()
toString(DateTimeFormatter dtf)
toString(String pattern)
toString(String pattern, Locale loc)
The first is a standard method in Java and so is included by default. The formatter-based method is a convenience as you could just use the formatter itself. The two pattern-based methods are a further convenience, as they simply create a formatter.
Note however that the pattern-based method requires two variants, one with and one without the locale, whereas the formatter embeds the locale internally (thus no need for a method variant). The need for the extra locale method weighed heavily when considering JSR-310.
An additional consideration is caching/performance. Joda-Time has a cache of patterns to formatters, to avoid re-parsing patterns like "yyyy-MM-dd". Such a cache was less welcome in the JDK and it was desirable to find a way to not have a cache.
The recommended approach for formatters with java.time is to assign them to static variables. The new formatter is thread-safe (unlike the old one) so this works well. In addition, defining a constant for the formatter means that the parsing of the pattern occurs only once - the constant stores the internal data structure ready to format/parse.
private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm:ss a z");
public static void main(String[] args) {
ZonedDateTime ldt = ZonedDateTime.now();
System.out.println(ldt.format(FORMATTER);
}
Finally, the method in JSR-310 was originally called toString(DateTimeFormatter)
. Internal reviews within Oracle recommended renaming the method to format(DateTimeFormatter)
.