how to convert a date dd / mm / yyyy to yyyy-MM-dd HH:mm:ss Android

Douglas Mesquita picture Douglas Mesquita · May 7, 2013 · Viewed 50.6k times · Source

How can I convert a date in dd / mm / yyyy to a format that supports sqlite yyyy-MM-dd'T'HH: mm: ss

for example:

public static String convertStringToData(String stringData)
        throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/aaaa");//yyyy-MM-dd'T'HH:mm:ss
    SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date data = sdf.parse(stringData);
    String formattedTime = output.format(data);
    return formattedTime;
}

Answer

fcm picture fcm · May 7, 2013
public static String formatDate (String date, String initDateFormat, String endDateFormat) throws ParseException {

    Date initDate = new SimpleDateFormat(initDateFormat).parse(date);
    SimpleDateFormat formatter = new SimpleDateFormat(endDateFormat);
    String parsedDate = formatter.format(initDate);

    return parsedDate;
}

This will return the parsed date as a String, with the format (both initial and end) as parameters to the method.