Joda-Time new DateTime() vs DateTime.now()

Viewed 3926

Since Joda-Time time version 2.0 the static method org.joda.time.DateTime#now() was introduced. To me it is not clear what is the benefit over the use of new DateTime() (as the code just delegates anyway).

public static DateTime now() {
    return new DateTime();
}

Also from the java doc it is not clear to me which one I should prefer.

new DateTime

Obtains a {@code DateTime} set to the current system millisecond time using ISOChronology in the default time zone.

DateTime#now()

Constructs an instance set to the current system millisecond time using ISOChronology in the default time zone.

Can someone explain in which use case which one should be preferred?

3 Answers

new DateTime() requires that a new object be allocated. DateTime.now can reuse a single object across requests, since DateTime instances are immutable. That may cause less memory churn.

But largely I doubt it matters which you use.

The now() method was added to make Joda-Time a little bit closer to java.time.* in Java 8, making the process of conversion a little bit easier. The two methods have exactly the same behaviour.

I don't think there is any difference. Using DateTime.now() looks more elegant than new DateTime() in your code. Here is DateTime.now() source code.

public static DateTime now() {
    return new DateTime();
}
Related