Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jodatime

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?

like image 258
brass monkey Avatar asked Sep 03 '18 11:09

brass monkey


2 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.

like image 97
T.J. Crowder Avatar answered Oct 16 '22 17:10

T.J. Crowder


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.

like image 1
JodaStephen Avatar answered Oct 16 '22 17:10

JodaStephen