Try to parse Date in format of Thu, 27 Sep 2012 13:44:09 +0000 or other format - Android java.util

Android examples for java.util:Date Parse

Description

Try to parse Date in format of Thu, 27 Sep 2012 13:44:09 +0000 or other format

Demo Code

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{

    public static String parseDate(String date) {
        // Thu, 27 Sep 2012 13:44:09 +0000
        SimpleDateFormat dfDb = new SimpleDateFormat(
                "EEE, dd MMM yyyy HH:mm:ss Z");
        Date dateDb = null;/*w w  w .jav a 2  s .c o  m*/
        try {
            dateDb = dfDb.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (dateDb == null) {
            // 27 Sep 2012 13:44:09 +0000
            dfDb = new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z");
            try {
                dateDb = dfDb.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        // 27/09/2012 13:44:09
        SimpleDateFormat toYours = new SimpleDateFormat(
                "dd/mm/yyyy HH:mm:ss");
        String yourString = toYours.format(dateDb);
        return yourString;
    }

}

Related Tutorials