Search code examples
javaandroidandroid-date

java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)


I want to parse the date into a desired format but i am receiving an exception every time. i know it is easy to implement but i am facing some problem don't know where exactly.:

Exception: java.text.ParseException: Unparseable date: "2014-06-04" (at offset 5)

Following is my code:

private String getconvertdate(String date) {
    DateFormat inputFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
    inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    DateFormat outputFormat = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
    Date parsed = null;
    try {
        parsed = inputFormat.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String outputText = outputFormat.format(parsed);
    return outputText;
}

Input to method: 2014-06-04

Expected Output: 06-Jun-2014

I have followed some Ref. from Stackoverflow.com, but still he problem persist. Please Help.


Solution

  • You have no time part in your string: and the Month has only two character replace

    new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss",Locale.ENGLISH);
    

    with

    new SimpleDateFormat("yyyy-MM-dd",Locale.ENGLISH);