用于获取字符串的Java Date对象构造函数已被弃用 [英] Java Date object constructor for getting string is deprecated
问题描述
我有一个字符串表示日期,例如String date =23/10/2012;
,我有一个函数获取日期作为参数,例如
public void foo(Date date){我的问题是接受字符串的Date构造函数已被弃用,我不想要使用
a弃用对象。解决方案所以,你看看不推荐使用的方法的Javadoc ,它指定: / p>
已弃用。从JDK 1.1版开始,由DateFormat.parse(String s)替换。
然后,您可以查找 DateFormat.parse
,然后进行相应的操作。您可能想要创建一个 SimpleDateFormat
- 或者更好的是,使用 Joda时间,这是一个更好的日期/时间API。
当一种方法被弃用时,总是一个好主意,指出现有的代码应该被迁移到,理想的原因是旧的方法被弃用。当您弃用自己的方法时,请遵循此指南...每当您尝试使用不推荐使用的方法时,请检查文档以查看是否提供了更好的选择。
I have problem using Date in java,
I have a string that represent a date for example String date = "23/10/2012";
and i have a function that get a date as parameter, for example
public void foo(Date date){...}
my problem is that Date constructor that accept a string is deprecated and i do not want to use
a deprecated object.
解决方案 So, you look at the Javadoc for the deprecated method which specifies:
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
You then look up DateFormat.parse
and proceed accordingly. You probably want to create a SimpleDateFormat
- or better still, use Joda Time which is a much better date/time API.
When a method is deprecated, it's always a good idea to give an indication of what existing code should be migrated to, and ideally why the old method was deprecated. Follow this guidance when you deprecate your own methods... and whenever you try to use a deprecated method, check the documentation to see if it suggests a better alternative.
这篇关于用于获取字符串的Java Date对象构造函数已被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!