brass monkey
brass monkey

Reputation: 6771

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

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?

Upvotes: 8

Views: 6856

Answers (3)

JodaStephen
JodaStephen

Reputation: 63385

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.

Upvotes: 3

B. S. Rawat
B. S. Rawat

Reputation: 1914

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();
}

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074295

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.

Upvotes: 6

Related Questions