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?
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:
Run Code Online (Sandbox Code Playgroud)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.
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |