如何在 Java 中获取当前日期/时间 [英] How to get the current date/time in Java

查看:47
本文介绍了如何在 Java 中获取当前日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中获取当前日期/时间的最佳方法是什么?

What's the best way to get the current date/time in Java?

推荐答案

这取决于你想要什么形式的日期/时间:

It depends on what form of date / time you want:

  • 如果您希望将日期/时间作为单个数值,则 System.currentTimeMillis() 为您提供,表示为 UNIX 时代之后的毫秒数(作为 Java).此值是 UTC 时间点的增量,与本地时区1无关.

  • 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). This value is a delta from a UTC time-point, and is independent of the local time-zone1.

如果您希望日期/时间的形式允许您以数字方式访问组件(年、月等),您可以使用以下方法之一:

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() 为您提供一个用当前日期/时间初始化的 Date 对象.问题在于 Date API 方法大多有缺陷......并且已弃用.

  • new Date() gives you a Date object initialized with the current date / time. The problem is that the Date API methods are mostly flawed ... and deprecated.

Calendar.getInstance() 为您提供一个 Calendar 对象,使用默认的 Locale 和当前日期/时间初始化时区.其他重载允许您使用特定的 Locale 和/或 TimeZone.日历有效……但 API 仍然很麻烦.

Calendar.getInstance() gives you a Calendar object initialized with the current date / time, using the default Locale and TimeZone. Other overloads allow you to use a specific Locale and/or TimeZone. Calendar works ... but the APIs are still cumbersome.

new org.joda.time.DateTime() 给你一个 Joda-time 对象用当前日期/时间初始化,使用默认时区和年表.还有很多其他的 Joda 替代品……太多了,无法在这里描述.(但请注意,有些人报告说 Joda 时间存在性能问题.;例如 https://stackoverflow.com/questions/6280829.)

new org.joda.time.DateTime() gives you a Joda-time object initialized with the current date / time, using the default time zone and chronology. There are lots of other Joda alternatives ... too many to describe here. (But note that some people report that Joda time has performance issues.; e.g. https://stackoverflow.com/questions/6280829.)

在 Java 8 中,调用 java.time.LocalDateTime.now()java.time.ZonedDateTime.now() 会给你表示2 当前日期/时间.

in Java 8, calling java.time.LocalDateTime.now() and java.time.ZonedDateTime.now() will give you representations2 for the current date / time.

在 Java 8 之前,大多数了解这些事情的人都推荐 Joda-time 作为拥有(迄今为止)最好的 Java API,用于处理时间点和持续时间计算.

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 及更高版本,建议使用标准的 java.time 包.Joda 时间现在被视为过时",Joda 维护人员建议人们迁移.3.

With Java 8 and later, the standard java.time package is recommended. Joda time is now considered "obsolete", and the Joda maintainers are recommending that people migrate.3.

1 - System.currentTimeMillis() 给出系统"时间.虽然将系统时钟设置为(标称)UTC 是正常做法,但本地 UTC 时钟和真实 UTC 之间会存在差异(增量).增量的大小取决于系统时钟与 UTC 同步的程度(以及频率).
2 - 请注意 LocalDateTime 不包括时区.正如 javadoc 所说:如果没有偏移量或时区等附加信息,它就无法表示时间线上的瞬间."
3 - 注意:如果您不迁移,您的 Java 8 代码不会中断,但 Joda 代码库最终可能会停止获取错误修复和其他补丁.截至 2020 年 2 月,正式的生命终结"即将到来.对于 Joda 尚未公布,Joda API 尚未标记为已弃用.

1 - System.currentTimeMillis() gives the "system" time. While it is normal practice for the system clock to be set to (nominal) UTC, there will be a difference (a delta) between the local UTC clock and true UTC. The size of the delta depends on how well (and how often) the system's clock is synced with UTC.
2 - Note that LocalDateTime doesn't include a time zone. As the javadoc says: "It cannot represent an instant on the time-line without additional information such as an offset or time-zone."
3 - Note: your Java 8 code won't break if you don't migrate, but the Joda codebase may eventually stop getting bug fixes and other patches. As of 2020-02, an official "end of life" for Joda has not been announced, and the Joda APIs have not been marked as Deprecated.

这篇关于如何在 Java 中获取当前日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆