lundi 29 juin 2015

The method parse(String) from the type Date is deprecated [duplicate]


This question already has an answer here:

I have the following snippit of code. Eclipse is telling me that "the method parse(String) from the type Date is deprecated". How can I fix my code so that I no longer get the deprecation warning?

String date = query.getString("somevariable");
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Date.parse(date));
setFireTime(cal.getTime());

My setFireTime method:

public void setFireTime(Date fireTime) {
   this.fireTime = fireTime;
}

After the suggestions below in the comment section, I have updated the code below:

String date = query.getString("fireTime");
Calendar cal = Calendar.getInstance();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.getDefault());
try {
    Date parsed = formatter.parse(date);            
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
cal.setTimeInMillis(parsed);
setFireTime(cal.getTime());

But I am getting parsed cannot be resolved to a variable error referring to cal.setTimeInMillis(parsed); now.


Aucun commentaire:

Enregistrer un commentaire