java - Joda-Time new DateTime() 与 DateTime.now()

标签 java jodatime

自 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/

相关文章:

java - 用Java解压压缩的ubuntu lz4文件

java - 是否需要覆盖注解 - Kotlin

java - EMMA 覆盖工具不显示逐行覆盖

java - 如何在 Joda-Time 中仅解析带时区的日期?

java - 在 joda time 如何在不更改时间的情况下转换时区

java - 如何将本地 dateTime 对象转换为 UTC java.util.Date

java - 将字符串时间戳转换为符合 ISO 8601 的字符串

java - 为什么下面的代码会导致线程争用

java - Python到java图像处理翻译

java - 在 Joda 中将毫秒转换为日期时间格式时出现解析错误