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

Nza*_*all 4 java date java-time

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"));
Run Code Online (Sandbox Code Playgroud)

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

Is there a significant difference between these 2?

Jon*_*eet 6

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);
Run Code Online (Sandbox Code Playgroud)

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