不推荐使用Date(String)

时间:2015-06-25 14:05:06

标签: java date deprecated

我的Date构造函数已弃用并以黄色突出显示。

如何使用Calendar.Set()解决此问题。我打电话给import java.util.Calendar;和date

代码如下。提前致谢。

Format f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date d = new Date(f.format(geoState.getString("fireTime")));
temp.setFireTime(d);

2 个答案:

答案 0 :(得分:2)

使用DateFormat.parse方法将String转换为Date

String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);

在你的情况下,它将是这样的

DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
Date date = format.parse(geoState.getString("fireTime"));
temp.setFireTime(date);

答案 1 :(得分:0)

将项目迁移到Java 11时遇到相同的问题。这是答案

新日期(“ 08/10/2020”);

DateFormat.getDateInstance()。parse(“ 08/10/2020”)