Java 不推荐使用构造函数 Date(...)。这是什么意思?(爪哇)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1999766/
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 constructor Date(...) is deprecated. What does it mean? (Java)
提问by snakile
I'm trying to create a Date
like this:
我正在尝试创建一个Date
这样的:
date = new Date(year-1900, mon-1, day, hrs, min, sec);
and Eclipse gives me this warning: "The constructor Date(int, int, int, int, int)
is deprecated".
Eclipse 给了我这个警告:“Date(int, int, int, int, int)
不推荐使用构造函数”。
What does it mean for a constructor to be deprecated, and what can I do?
不推荐使用构造函数意味着什么,我该怎么办?
采纳答案by JuanZe
Deprecated literally means disapproved of, but a more accurate translation would be retired. Deprecated means this method is still usable, but you should not use it. It will gradually be phased out. There is a new method to do the same thing. Deprecated methods are marked with a special Javadoc comment:
Deprecated 字面意思是不赞成,但更准确的翻译将被淘汰。弃用意味着此方法仍然可用,但您不应使用它。将逐步淘汰。有一种新方法可以做同样的事情。不推荐使用的方法用特殊的 Javadoc 注释标记:
/**
*@deprecated Please now use newMethod()
*@see newMethod()
*/
Use:
使用:
Calendar.set(year + 1900, month, date, hrs, min)
Calendar.set(year + 1900, month, date, hrs, min)
or
或者
GregorianCalendar(year + 1900, month, date, hrs, min)
.
GregorianCalendar(year + 1900, month, date, hrs, min)
.
As suggested by the API documentation.
回答by Daniel A. White
Deprecated means that it is a legacy or old way to do something and it should be avoided.
弃用意味着它是一种遗留或旧的做事方式,应该避免。
According to this document http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html, use Calendar.set(...)
.
根据这个文档http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html,使用Calendar.set(...)
.
回答by Paul McKenzie
As it is deprecated means that you ought not really use it. You could use Calendar to generate a date from fields instead.
因为它已被弃用意味着您不应该真正使用它。您可以使用 Calendar 从字段中生成日期。
回答by Jon Skeet
It means you shouldn't use it in new code. This is typically the case if there's now a better way of achieving something, but the old way is maintained for backward compatibility.
这意味着您不应该在新代码中使用它。如果现在有更好的方法来实现某事,通常就是这种情况,但为了向后兼容而保留旧的方法。
Instead, you coulduse the Calendar
API, as the full message hopefully suggests to you - or (better IMO) you could use Joda Timeor the java.time
package in Java 8 (see the tutorial). Both of those are farsuperior date/time APIs. to the
相反,您可以使用Calendar
API,正如完整消息希望向您建议的那样 - 或者(更好的 IMO)您可以使用Joda Time或java.time
Java 8 中的包(请参阅教程)。这两个都是非常优越的日期/时间 API。到
When it comes to deprecated APIs, if the compiler message doesn'tsuggest an alternative, it's always worth looking at the Javadoc - which in this case suggests using Calendar.set(...)
.
当涉及已弃用的 API 时,如果编译器消息没有建议替代方案,则始终值得查看 Javadoc - 在这种情况下建议使用Calendar.set(...)
.
回答by missingfaktor
That means you shouldn't be using it in new code typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.
这意味着您不应该在新代码中使用它,因为它很危险,或者因为存在更好的替代方案。当在非弃用代码中使用或覆盖已弃用的程序元素时,编译器会发出警告。
In your case, you can use java.util.Calendar
class instead of java.util.Date
.
在您的情况下,您可以使用java.util.Calendar
class 而不是java.util.Date
.
By the way, in Java 8 and later, these old classes are supplanted by the new java.time package(Tutorial). Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen Extraproject. The old classes remain in place and you may continue to use them (while avoiding their deprecated parts), but you are encouraged to transition to the new classes.
顺便说一下,在 Java 8 及更高版本中,这些旧类被新的java.time 包(教程)取代。受Joda-Time 的启发,由JSR 310定义,并由ThreeTen Extra项目扩展。旧类仍然存在,您可以继续使用它们(同时避免它们被弃用的部分),但我们鼓励您过渡到新类。
回答by jalf
Deprecated generally means that you're discouraged from using the function.
弃用通常意味着不鼓励您使用该函数。
For some reason or another, it has been decided that Java would be better off without it (because a better alternative exists, and you should use that instead), and so it mightbe removed from a future version of Java. Deprecation is basically a warning that "this will probably get removed in the future, although we're keeping it around a bit longer to give you a chance to remove it from your code first"
出于某种原因,已经决定没有它 Java 会更好(因为存在更好的替代方案,您应该使用它),因此它可能会从 Java 的未来版本中删除。弃用基本上是一个警告,“这可能会在未来被删除,尽管我们将其保留更长时间,以便您有机会首先从代码中删除它”
回答by EJB
deprecated means the usage of this constructor is discouraged, and it may be removed in future releases of Java. Use the Calendar API.
弃用意味着不鼓励使用此构造函数,并且可能会在 Java 的未来版本中将其删除。使用日历 API。
回答by ChrisCantrell
Here is a code snippet to help with migrating your code. Both prints are the same.
这是一个有助于迁移代码的代码片段。两幅画都是一样的。
import java.util.Calendar;
import java.util.Date;
public class Tinker {
public static void main(String[] args) {
int Y = 2015; // Year 2015
int M = 11; // 0..11 -- December
int D = 15; // 15th
int H = 16; // 4:00 PM
int MN = 28; // 4:28 PM
int S = 41; // 4:28:41
Date d = new Date(Y-1900,M,D,H,MN,S);
System.out.println(d);
Calendar c = Calendar.getInstance();
c.set(Y, M, D, H, MN, S);
d = c.getTime();
System.out.println(d);
}
}
The output:
输出:
Tue Dec 15 16:28:41 CST 2015
Tue Dec 15 16:28:41 CST 2015
回答by Premraj
Deprecated meansthe core API and other java libraries are not depends on it and it says you have better way(s).
弃用意味着核心 API 和其他 Java 库不依赖于它,它表示您有更好的方法。
回答by Ole V.V.
java.time
时间
…and what can I do?
……我能做什么?
- Decide on a time zone.
- Use java.time, the modern Java date and time API.
- 决定一个时区。
- 使用 java.time,现代 Java 日期和时间 API。
For example:
例如:
ZoneId zone = ZoneId.of("America/Glace_Bay");
ZonedDateTime dateTime = ZonedDateTime.of(2019, 10, 27, 12, 34, 56, 0, zone);
System.out.println(dateTime);
Output is:
输出是:
2019-10-27T12:34:56-03:00[America/Glace_Bay]
2019-10-27T12:34:56-03:00[美国/Glace_Bay]
Don't use Date
. That class was always poorly designed. There is a reason why most constructors and most methods were deprecated very quickly. They don't work reliably across time zones. Today we have so much better in java.time.
不要使用Date
. 那个班级的设计总是很糟糕。大多数构造函数和大多数方法很快被弃用是有原因的。它们不能跨时区可靠地工作。今天,我们在 java.time 上有了更好的表现。
Only if you need a Date
for a legacy API that you cannot afford to upgrade to java.time just now, convert like this:
仅当您需要一个Date
用于您现在无法升级到 java.time 的遗留 API 时,请按如下方式转换:
Instant pointInTime = dateTime.toInstant();
Date oldFashionedDate = Date.from(pointInTime);
System.out.println(oldFashionedDate);
Output in my time zone is:
我时区的输出是:
Sun Oct 27 15:34:56 GMT 2019
格林威治标准时间 2019 年 10 月 27 日星期日 15:34:56
The time of day doesn't agree with what we specified. This is because I am in a different time zone. Date.toString
(called from System.out.println
) renders the date in the default time zone of the JVM, which has confused many over the years (it's just one of the many reasons to avoid that class). The Date
does represent the correct point in time, and you can use it for your legacy API.
一天中的时间与我们指定的不一致。这是因为我在不同的时区。Date.toString
(调用 from System.out.println
)在 JVM 的默认时区中呈现日期,多年来这让很多人感到困惑(这只是避免该类的众多原因之一)。该Date
确实代表了正确的时间点,你可以用它为你的遗留API。
What does it mean for a constructor to be deprecated,…
不推荐使用构造函数是什么意思,...
Many have explained that nicely already. I haven't taken the deprecation from the 1990s as a promise to remove the constructor. In Java 9 they introduced the possibility of marking a deprecated item for removal. I am still curious to see whether they will mark this constructor for removal at some point.
许多人已经很好地解释了这一点。我没有将 1990 年代的弃用作为删除构造函数的承诺。在 Java 9 中,他们引入了将已弃用的项目标记为删除的可能性。我仍然很想知道他们是否会在某个时候将此构造函数标记为删除。
Links
链接
- Oracle tutorial: Date Timeexplaining how to use java.time.
- Enhanced Deprecationin Oracle JDK 9 Documentation.
- Oracle 教程:解释如何使用 java.time 的日期时间。
- 增强弃用在甲骨文JDK 9文档。