不推荐使用 java.util.Date 类的参数化构造函数。什么是替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1874298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The parameterized constructors of the java.util.Date class are deprecated. What is the alternative?
提问by Tim
What can I use to replace this, new Date(2009, 12, 9)
?
我可以用什么来代替这个,new Date(2009, 12, 9)
?
Thanks for your help.
谢谢你的帮助。
采纳答案by Jon Skeet
Note: this answer was written in 2009. Since then, java.time
has become the preferred date/time API in Java.
注意:这个答案写于 2009 年。从那时起,java.time
它就成为 Java 中首选的日期/时间 API。
Ideally, use Joda Timeinstead. It's an infinitely superior API to the built-in one. You'd then want to choose between LocalDateTime
and DateTime
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 docsdo a good job).
理想情况下,请改用Joda Time。它是一种比内置 API 无限优越的 API。那么你要选择之间LocalDateTime
,并DateTime
根据您的具体要求(这是一个复杂的领域-我不打算尝试在一两句话来概括,但文档做好)。
If absolutely necessary, use a java.util.Calendarand convert that to a Date
when you need to.
如果绝对必要,请使用java.util.Calendar并Date
在需要时将其转换为 a 。
回答by pjp
If you look at the Javadocit points you towards using Calendar.
As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date, hrs, min) or GregorianCalendar(year + 1900, month, date, hrs, min).
从 JDK version 1.1 开始,由 Calendar.set(year + 1900, month, date, hrs, min) 或 GregorianCalendar(year + 1900, month, date, hrs, min) 替换。
If you look at the Date
constructor params you'll see why it was deprecated:
如果您查看Date
构造函数参数,您就会明白为什么不推荐使用它:
Parameters:
year - the year minus 1900. month - the month between 0-11. date - the day of the month between 1-31. hrs - the hours between 0-23. min - the minutes between 0-59.
参数:
year - the year minus 1900. month - the month between 0-11. date - the day of the month between 1-31. hrs - the hours between 0-23. min - the minutes between 0-59.
year
isn't what you expect and neither is month
.
year
不是你所期望的,也不是month
。
To represent the date you have mentioned you need to call Date
like this (not recommended)
要代表您提到的日期,您需要Date
像这样打电话(不推荐)
new Date(2009-1900, 12-1, 9)
The alternative using Calendar
is
使用的替代方法Calendar
是
Calendar cal = Calendar.getInstance();
cal.set(2009, 11, 9); //year is as expected, month is zero based, date is as expected
Date dt = cal.getTime();
回答by Dave
GregorianCalendar(year + 1900, month, date)
回答by OscarRyz
You can also use the SimpleDateFormatobject:
您还可以使用SimpleDateFormat对象:
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class DateTest {
public static void main( String [] args ) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
Date date = sdf.parse("2009, 12, 9");
System.out.println( date );
}
}
回答by mcv
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.
不幸的是,Java 中的日期支持非常糟糕。正式地说,您可能必须使用 Calendar 执行此操作,但在我看来,这没有必要。就像其他人提到的那样,Joda 时间要好得多,但仍然不如 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.
我不知道有任何 Java 包可以为您提供相当多的日期支持(Joda 有点不足,但很接近),但是在 Groovy 中,使用 TimeCategory 可以为您提供非常类似于 Ruby on Rails 的日期支持。
回答by Maxime ARNSTAMM
Calendar cal = Calendar.getInstance();
cal.set(2009, Calendar.DECEMBER, 12);
Notice that i didn't put 12 for december because it's actually 11 (january is 0).
请注意,我没有将 12 放在 12 月,因为它实际上是 11(一月是 0)。
Then, you can add or remove seconds, minutes, hours, days, months or year easily with :
然后,您可以使用以下命令轻松添加或删除秒、分、小时、天、月或年:
cal.add(Calendar.HOUR, 2);
cal.add(Calendar.MONTH, -5);
And finally, if you want a Date :
最后,如果你想要一个 Date :
cal.getTime();
回答by Nick Hristov
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).
蒂姆,在您的评论中,您提到您是在 GXT 上下文中执行此操作 - 即在 GWT 客户端代码中。GWT 不支持 GregorianCalendar 并且您很可能无法通过 GWTCompiler 放置 JodaTime(您可能可以,但您真的想要)。
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.
如果您想在 GWT 中进行日历操作,我认为您可以选择使用 JNSI。请参阅 JavaScript 中的 Date 类。