Error using date type on android

Asked

Viewed 47 times

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 , he says dt is not correct because it is a string.

1 answer

1


dtis 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 that:

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.

  • 1

    Thank you very much! Just to point out that I needed to do Date data=null;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.