What is the difference between LocalDateTime.format(DateTimeFormatter) and DateTimeFormatter.format(TemporalAccessor)?

Nzall :

In the Java 8 DateTime API, there are 2 ways to format a date that at first glance seem to do the same thing:

DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm").format(LocalDateTime.now());
LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));

These both return a String representing the date and time at runtime.

Is there a significant difference between these 2?

Jon Skeet :

No, they're equivalent - and they're even documented to be equivalent, at least for the default implementation. ChronoLocalDateTime.format includes this:

The default implementation must behave as follows:

return formatter.format(this);

Sometimes one form is useful, sometimes the other is - it can depend on what you're chaining together in a longer expression.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=121725&siteId=1