java.util.Date 类的参数化构造函数已弃用。还有什么选择呢?
我可以用什么来替换这个,new Date(2009, 12, 9)
?
感谢您的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以用什么来替换这个,new Date(2009, 12, 9)
?
感谢您的帮助。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
注意:这个答案写于 2009 年。从那时起,
java.time
就成为 Java 中首选的日期/时间 API。理想情况下,请使用 Joda 时间。它是一种比内置 API 无限优越的 API。然后,您需要在
LocalDateTime 之间进行选择
和DateTime
取决于您的具体要求(这是一个复杂的领域 - 我不会尝试用一两句话来总结,但是 文档 做得很好)。如果绝对必要,请使用 java.util.Calendar< /a> 并在需要时将其转换为
Date
。Note: this answer was written in 2009. Since then,
java.time
has become the preferred date/time API in Java.Ideally, use Joda Time instead. It's an infinitely superior API to the built-in one. You'd then want to choose between
LocalDateTime
andDateTime
depending on your exact requirements (it's a complicated area - I'm not going to try to summarise in a sentence or two, but the docs do a good job).If absolutely necessary, use a java.util.Calendar and convert that to a
Date
when you need to.日历!
请注意,我没有'不要将 12 表示为 12 月,因为它实际上是 11(一月是 0)。
然后,您可以使用以下命令轻松添加或删除秒、分钟、小时、天、月或年:
最后,如果您想要日期:
Calendar !
Notice that i didn't put 12 for december because it's actually 11 (january is 0).
Then, you can add or remove seconds, minutes, hours, days, months or year easily with :
And finally, if you want a Date :
如果您查看 Javadoc 它指导您使用日历。
如果您查看
Date
构造函数参数,您就会明白为什么它被弃用:year
不是您所期望的,month
也不是您所期望的。要表示您提到的日期,您需要像这样调用
Date
(不推荐)使用
Calendar
的替代方法是If you look at the Javadoc it points you towards using Calendar.
If you look at the
Date
constructor params you'll see why it was deprecated:year
isn't what you expect and neither ismonth
.To represent the date you have mentioned you need to call
Date
like this (not recommended)The alternative using
Calendar
is您还可以使用 SimpleDateFormat 对象:
You can also use the SimpleDateFormat object:
Tim,在您的评论中您提到您是在 GXT 上下文中执行此操作 - 即在 GWT 客户端代码中。 GWT 不支持 GregorianCalendar,您很可能无法通过 GWTCompiler 放置 JodaTime(您也许可以,但您真的想这样做吗)。
我认为如果您想在 GWT 中进行日历操作,您确实可以选择使用 JNSI。请参阅 JavaScript 中的 Date 类。
Tim, in your comments you mentioned that you are doing this in a GXT context - i.e. in GWT client code. GWT does not support GregorianCalendar and you will most likely not be able to put JodaTime through the GWTCompiler (you may be able to, but do you really want to).
I think you are left really with the option to using JNSI if you want to do calendar operations in GWT. See the Date class in JavaScript.
java.time
java.util
Date-Time API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*。另外,下面引用的是 Joda-Time 主页上的通知:
使用现代 API
java.time
的解决方案:输出:
输出中的
Z
是 零时区偏移的时区指示符。它代表 Zulu,指定Etc/UTC
时区(时区偏移量为+00:00
小时)。无论出于何种原因,如果您需要将
ZonedDateTime
对象转换为java.util.Date
对象,可以按如下方式操作:在线演示
就像
java.util.Date
一样,一个Instant
表示UTC 中的时间线,但与java.util.Date
不同,它并不代表真正的日期时间对象(java.util.Date
对象仅代表自标准基准时间(称为“纪元”)以来的毫秒数,即January 1 , 1970, 00:00:00 GMT
),它代表一个真实的日期时间对象。了解有关
java.time
的更多信息,现代日期时间 API* 来自 跟踪:日期时间。* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API 和 如何在Android项目中使用ThreeTenABP。
java.time
The
java.util
Date-Time API and their formatting API,SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.Also, quoted below is a notice at the Home Page of Joda-Time:
Solution using
java.time
, the modern API:Output:
The
Z
in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies theEtc/UTC
timezone (which has the timezone offset of+00:00
hours).For any reason, if you need to convert this object of
ZonedDateTime
to an object ofjava.util.Date
, you can do so as follows:ONLINE DEMO
Like
java.util.Date
, anInstant
represents an instantaneous point on the timeline in UTC, but unlikejava.util.Date
, which does not represent a real Date-Time object (ajava.util.Date
object simply represents the number of milliseconds since the standard base time known as "the epoch", namelyJanuary 1, 1970, 00:00:00 GMT
), it represents a real Date-Time object.Learn more about
java.time
, the modern Date-Time API* from Trail: Date Time.* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
不幸的是,Java 中的日期支持非常糟糕。正式地,您可能必须使用日历来执行此操作,但在我看来这不是必需的。正如其他人提到的,Joda 时间要好得多,但仍然不如 Ruby on Rails 中的日期那么容易使用。
我不知道有哪个 Java 包可以为您提供如此多的日期支持(Joda 稍有不足,但已经很接近了),但是在 Groovy 中,使用 TimeCategory 可以为您提供非常类似于 Ruby on Rails 的日期支持。
Unfortunately date support in Java is completely awful. Officially, you'd probably have to do this with Calendar, but that shouldn't be necessary in my opinion. Like others have mentioned, Joda time is a lot better, but still not quite as easy to use as dates in Ruby on Rails.
I'm not aware of any Java package that gives you quite that amount of date support (Joda falls short a bit, but comes close), but in Groovy, using TimeCategory gives you very Ruby on Rails-like date support.
https://docs.oracle.com/ javase/1.5.0/docs/api/java/util/Date.html
https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html