Change date string format in android

This will do it:

public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){

    Date parsed = null;
    String outputDate = "";

    SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
    SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());

    try {
        parsed = df_input.parse(inputDate);
        outputDate = df_output.format(parsed);

    } catch (ParseException e) { 
        LOGE(TAG, "ParseException - dateFormat");
    }

    return outputDate;

}

Example:

String date_before = "1970-01-01";
String date_after = formateDateFromstring("yyyy-MM-dd", "dd, MMM yyyy", date_before);

Output:

date_after = "01, Jan 1970";

SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy  hh:mm a");
String date = format.format(Date.parse("Your date string"));

UPDATE :-

As on, Date.parse("Your date string"); is deprecated.

String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);

format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);

Tags:

Android