自 Joda-Time 时间版本 2.0 以来,引入了静态方法 org.joda.time.DateTime#now()
。
对我来说,不清楚使用 new DateTime()
有什么好处(因为代码只是委托(delegate))。
public static DateTime now() {
return new DateTime();
}
同样从 java 文档来看,我不清楚我应该更喜欢哪一个。
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.
有人可以解释在哪种用例中应该首选哪个吗?
最佳答案
new DateTime()
要求分配一个新对象。 DateTime.now
可以跨请求重用单个对象,因为 DateTime
实例是不可变的。这可能会减少内存流失。
但在很大程度上,我怀疑你使用的是什么。
关于java - Joda-Time new DateTime() 与 DateTime.now(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52148892/