Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.text.ParseException: Unparseable date

Tags:

date

android

In my project, I am trying to parse date format like this "Mon Oct 20 00:00:00 GMT+06:30 2014" to dd-MM-yyyy but I got the following error. I am hoping someone to solve me this problem.

Thanks,

10-20 13:03:01.390: W/System.err(23409): java.text.ParseException: Unparseable date: "Mon Oct 20 00:00:00 GMT+06:30 2014" (at offset 0)

parseDate.java

SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");
String sdate="Mon Oct 20 00:00:00 GMT+06:30 2014";
    try {
        Date _date= formatter_date.parse(sdate);
        holder.txtDate.setText(String.valueOf(_date));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
like image 542
SAWAUNG Avatar asked Oct 20 '14 07:10

SAWAUNG


2 Answers

use this code

public static String parseTodaysDate(String time) {



    String inputPattern = "EEE MMM d HH:mm:ss zzz yyyy";

    String outputPattern = "dd-MM-yyyy";

    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);

        Log.i("mini", "Converted Date Today:" + str);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}
like image 182
Meenal Avatar answered Sep 21 '22 01:09

Meenal


Replace

SimpleDateFormat formatter_date = new SimpleDateFormat("dd-MM-yyyy");

with

SimpleDateFormat formatter_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
like image 32
cosmincalistru Avatar answered Sep 24 '22 01:09

cosmincalistru