AskingToStack AskingToStack - 5 months ago 21
Android Question

Converting Date "2016-06-03" to "03 JUNE 2016" in Android

I have date format as

"2016-06-03"
and I have to convert it as below :

"03 JUNE 2016"
.

I have tried as below :

SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
String newFormat = formatter.format("2016-06-03");


But, getting below error :

Invalid Arguments Exception


Please, help me to solve out this. Thanks.

Answer

Try This common function for convert date format

public static SimpleDateFormat targetFormat = new SimpleDateFormat();
public static SimpleDateFormat originalFormat = new SimpleDateFormat();
public static String formattedDate = "";

public static String getFormattedDate(String targetPattern,
                                      String existingPattern, String existingValue) {
    formattedDate = existingValue;
    targetFormat.applyPattern(targetPattern);
    DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
    symbols.setAmPmStrings(new String[] { "AM", "PM" });
    targetFormat.setDateFormatSymbols(symbols);
    originalFormat.applyPattern(existingPattern);
    try {
        formattedDate = targetFormat.format(originalFormat
                .parse(existingValue));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return formattedDate;
}

and used like

 String txtdate = getFormattedDate("dd MMMM yyyy","yyyy-MM-dd","2016-06-03");
Comments