Error using date type on android

2

I have this code:

    String dt = "2017-01-04";  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    try {
        sdf.parse(dt);
    } catch (ParseException e) {
        e.printStackTrace();
    }

But when I use dt, in a method that requires java.util.date, it says no dt is not correct because it is a string.

    
asked by anonymous 06.07.2017 / 15:49

1 answer

1

dt is a String. It was declared like this:

String dt = "2017-01-04";  

What you should use is the Date object returned by:

sdf.parse(dt);

Something like this:

String dt = "2017-01-04";  
Date data = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
    data = sdf.parse(dt);
} catch (ParseException e) {
    e.printStackTrace();
}

Pass the variable data to the method that requires java.util.date.

    
06.07.2017 / 15:55