什么是替换 java.util.Date(year,month,day) 的公认方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2687081/
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
What is the accepted way to replace java.util.Date(year,month,day)
提问by dagw
I'm trying to do something really simple, but starting to realize that dates in Java are a bit of minefield. All I want is to get passed groups of three ints ( a year, a month and a date) create some Date
objects, do some simple test on them (along the lines of as date A before date B and after January 1 1990), convert them to java.sql.Date
objects and pass them off to the database via JDBC.
我正在尝试做一些非常简单的事情,但开始意识到 Java 中的日期有点雷区。我想要的只是通过三个整数组(一年、一个月和一个日期)创建一些Date
对象,对它们进行一些简单的测试(沿着日期 B 之前和 1990 年 1 月 1 日之后的日期 A),转换将它们java.sql.Date
传递给对象并通过 JDBC 传递给数据库。
All very simple and works fine using the java.util.Date(int year,int month,int day)
constructor. Of course that constructor is depreciated, and I'd like to avoid using depreciated calls in new code I'm writing. However all the other options to solve this simple problem seem stupidly complicated. Is there really no simple way to do what I want without using depreciated constructors?
一切都非常简单,使用java.util.Date(int year,int month,int day)
构造函数可以正常工作。当然,该构造函数已折旧,我想避免在我正在编写的新代码中使用折旧的调用。然而,解决这个简单问题的所有其他选项似乎都非常复杂。如果不使用折旧的构造函数,真的没有简单的方法可以做我想做的事吗?
I know the standard answer to all Java date related questions is "use joda time", but I really don't want to start pulling in third party libraries for such a seemingly trivial problem.
我知道所有 Java 日期相关问题的标准答案是“使用 joda 时间”,但我真的不想开始为这样一个看似微不足道的问题引入第三方库。
采纳答案by Jo?o Silva
The idea is to use the Calendar
class, like so:
这个想法是使用Calendar
该类,如下所示:
Calendar cal = Calendar.getInstance();
cal.set(year, month, date);
Date date = cal.getTime();
Indeed, if you check the Javadoc of the constructoryou are mentioning, it is exactly what is suggested:
事实上,如果你检查你提到的构造函数的 Javadoc ,它正是建议的:
Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + 1900, month, date) or GregorianCalendar(year + 1900, month, date).
Or ... use JodaTime :-).
或者......使用JodaTime :-)。
回答by Yishai
Well, you can use the deprecated constructor. It has been deprecated for over 13 years and still works - it isn't going anywhere. If you don't want to do that and don't want to use a third party library, you have to use Calendar.
好吧,您可以使用已弃用的构造函数。它已被弃用超过 13 年,但仍然有效 - 它不会去任何地方。如果您不想这样做并且不想使用第三方库,则必须使用日历。
In Java 7, hopefully there will be a new time API, and you won't have the need for a third party API anymore.
在 Java 7 中,希望会有一个新的时间 API,您将不再需要第三方 API。
回答by artgon
LG's answer is valid but in general you should use Calendar
for all your date operations and leave Date
out of it unless your API requires it explicitly. In that case you can just convert it when passing it to the API.
LG 的回答是有效的,但一般来说,除非您的 API 明确要求,否则您应该将其Calendar
用于所有日期操作并忽略Date
它。在这种情况下,您可以在将其传递给 API 时对其进行转换。