It depends on what form of date / time you want:
(这取决于您想要的日期/时间形式:)
If you want the date / time as a single numeric value, then System.currentTimeMillis()
gives you that, expressed as the number of milliseconds after the UNIX epoch (as a Java long
).
(如果您希望日期/时间为单个数值,那么System.currentTimeMillis()
会为您提供该值,以UNIX纪元后的毫秒数表示(以Java long
)。)
This value is a delta from a UTC time-point, and is independent of the local time-zone ... assuming that the system clock has been set correctly. (此值是UTC时间点的增量,并且与本地时区无关...假设系统时钟已正确设置。)
If you want the date / time in a form that allows you to access the components (year, month, etc) numerically, you could use one of the following:
(如果您希望日期/时间的格式允许您以数字方式访问组件(年,月等),则可以使用以下方式之一:)
new Date()
gives you a Date
object initialized with the current date / time.
(new Date()
为您提供了一个使用当前日期/时间初始化的Date
对象。)
The problem is that the Date
API methods are mostly flawed ... and deprecated. (问题在于Date
API方法大多存在缺陷……并且已过时。)
Calendar.getInstance()
gives you a Calendar
object initialized with the current date / time, using the default Locale
and TimeZone
.
(Calendar.getInstance()
使用默认的Locale
和TimeZone
为您提供一个使用当前日期/时间初始化的Calendar
对象。)
Other overloads allow you to use a specific Locale
and/or TimeZone
. (其他重载允许您使用特定的Locale
和/或TimeZone
。)
Calendar works ... but the APIs are still cumbersome. (日历可以工作...但是API仍然很麻烦。)
new org.joda.time.DateTime()
gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology.
(new org.joda.time.DateTime()
使用默认的时区和年表为您提供了一个使用当前日期/时间初始化的Joda-time对象。)
There are lots of other Joda alternatives ... too many to describe here. (Joda还有许多其他替代方案,在此无法描述。)
(But note that some people report that Joda time has performance issues.; eg Jodatime's LocalDateTime is slow when used the first time .) ((但请注意,有些人报告说Joda时间存在性能问题。例如,第一次使用Jodatime的LocalDateTime速度很慢 。))
in Java 8, calling LocalDateTime.now()
and ZonedDateTime.now()
will give you representations 1 for the current date / time.
(在Java 8中,调用LocalDateTime.now()
和ZonedDateTime.now()
将为您提供当前日期/时间的表示形式1 。)
Prior to Java 8, most people who know about these things recommended Joda-time as having (by far) the best Java APIs for doing things involving time point and duration calculations.
(在Java 8之前,大多数了解这些事情的人都建议Joda-time拥有(到目前为止)最好的Java API来完成涉及时间点和持续时间计算的事情。)
With Java 8, this is no longer true. (对于Java 8,这不再是事实。)
However, if you are already using Joda time in your codebase, there is no strong 2 reason to migrate. (但是,如果您已经在代码库中使用Joda time,则没有2个强烈的理由进行迁移。)
1 - Note that LocalDateTime doesn't include a time zone.
(1-请注意,LocalDateTime不包含时区。)
As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone." (正如javadoc所说: “如果没有其他信息(例如偏移量或时区),它就无法在时间轴上表示时刻。”)
2 - Your code won't break if you don't, and you won't get deprecation warnings.
(2-如果不这样做,您的代码不会中断,也不会收到弃用警告。)
Sure, the Joda codebase will probably stop getting updates, but it is unlikely to need them. (当然,Joda代码库可能会停止获取更新,但是不太可能需要它们。)
No updates means stability and that is a good thing. (没有更新意味着稳定性,这是一件好事 。)
Also note that it is highly likely that if that someone will fix problems caused by regressions in the Java platform. (还要注意,很有可能有人会修复Java平台中由回归引起的问题。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…