5

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();
    }
2
  • can we see the declaration of formatter_date? Commented Oct 20, 2014 at 7:27
  • @cosmincalistru I just added formatter_date, thank you! Commented Oct 20, 2014 at 7:28

3 Answers 3

8

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@MeenalSharma,I think you miss one 'd' inputPattern.
@MeenalSharma , change your parsing format dd instead of d
NB : if the error persists you have to add the local information pour the inputFormat. SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH);
4

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);

2 Comments

Because that's the way the string is made. The input value would have to be formatted that way. But the input is not "20-10-2014"...
Great answer, simply true!
2

Unfortunately, the accepted answer has missed a crucial thing, Locale while parsing the date-time string. Therefore, that code will work merely by coincidence - only when the JVM on which it is executed, has an English Locale set. Check Always specify a Locale with a date-time formatter for custom formats to learn more about it.

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.

Solution using modern date-time API

Your date-time string has a time zone offset. So, parse your date-time string into an OffsetDateTime using DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu", Locale.ENGLISH) and format it using DateTimeFormatter.ofPattern("dd-MM-uuuu", Locale.ENGLISH).

Demo:

public class Main {
    private static final DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss OOOO uuuu",
            Locale.ENGLISH);

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu",
            Locale.ENGLISH);

    public static void main(String[] args) {
        OffsetDateTime odt = OffsetDateTime.parse("Mon Oct 20 00:00:00 GMT+06:30 2014", parser);
        System.out.println(odt);

        // Formatted output
        System.out.println(odt.format(formatter));
    }
}

Output:

2014-10-20T00:00+06:30
20-10-2014

Online Demo

Note: For whatever reason, if you need an instance of java.util.Date from this object of OffsetDateTime, you can do so as follows:

java.util.Date date = Date.from(odt.toInstant());

Learn more about the modern date-time API from Trail: Date Time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.