androidlive
androidlive

Reputation: 103

The Date(String) is deprecated

My Date constructor is deprecated and highlighted in Yellow.

How can I use Calendar.Set() to resolve this issue. I have called both import java.util.Calendar; and date.

Code is below. Thanks in advance.

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

Upvotes: 2

Views: 10939

Answers (2)

Vasanth Umapathy
Vasanth Umapathy

Reputation: 1741

I have the same issue while migrating my project to Java 11. Here is the answer

new Date("08/10/2020");

to

DateFormat.getDateInstance().parse("08/10/2020")

Upvotes: 0

Mauricio Gracia Gutierrez
Mauricio Gracia Gutierrez

Reputation: 10862

Use a DateFormat.parse method to convert a String to Date

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

In your case it will be something like this

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

Upvotes: 5

Related Questions