user3401369 user3401369 - 5 months ago 13
Java Question

Android: How to convert string into date

I am currently working on a calendar application using Caldroid and the date that Caldroid returns looks like this:

Fri Jun 17 12:31:00 GMT+07:00 2016


I just need to extract the date from it like for example:

17-06-2016


How am I supposed to do that?

Answer

Try this -

    DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");

    try {
        //This will give you the desired output
        df2.format(df.parse("Fri Jun 17 12:31:00 GMT+07:00 2016"));
    } catch (ParseException e) {            
        e.printStackTrace();
    }