2

How can I parse this date format Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest) to this date format 05-14-2010 I mean mm-dd-yyyy

it's telling me this error :

java.text.ParseException: Unparseable date: "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"

EDIT

SimpleDateFormat formatter = new SimpleDateFormat("M-d-yyyy");
newFirstDate = formatter.parse(""+vo.getFirstDate());  //here the error

Thanks in advance!

7
  • 3
    Please show the code you've been trying to use. Commented May 23, 2012 at 18:45
  • What's the return type and value of vo.getFirstDate()? Commented May 23, 2012 at 18:54
  • I have edited my post. vo.getFirstDate() returns a date in the first format :Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest) Commented May 23, 2012 at 18:54
  • If it returns a java.util.Date there's no need to parse() and all you need is new SimpleDateFormat("MM-dd-yyyy").format(vo.getFirstDate()). Commented May 23, 2012 at 19:18
  • 2
    @PhilippReichart That ""+... is very suspicious, though. Why would he add that if he already had a String? Commented May 23, 2012 at 19:51

2 Answers 2

5

This code first adapts the string a bit and then goes on to parse it. It respects the timezone, just removes "GMT" because that's how SimpleDateFormat likes it.

final String date = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)"
  .replaceFirst("GMT", "");
System.out.println(
    new SimpleDateFormat("MM-dd-yyyy").format(
        new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z").parse(date)));

Prints:

05-14-2010

Bear in mind that the output is also timezone-sensitive. The instant defined by your input string is being interpreted in my timezone as belonging to the date that the program printed. If you just need to transform "May 14 2010" into "05-14-2010", that's another story and SimpleDateFormat is not well suited for that. The JodaTime library would handle that case much more cleanly.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, same error : java.text.ParseException: Unparseable date: "Mon May 14 2012 00:00:00 +0100"
@adil I ran this code and didn't get the exception. The code prints as the answer states. This is Java 6, BTW. I see from your message that you obviously didn't run my code, but some version of your own. Since I don't know what exact code you ran to get the exception, there's little I can do about it.
@adil Now I parsed the exact string you complain about in the comment above: System.out.println(new SimpleDateFormat("MM-dd-yyyy").format(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss Z").parse("Mon May 14 2012 00:00:00 +0100"))); and it prints 05-14-2012.
2
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test
{
    public static void main( String args[] ) throws ParseException
    {
        // Remove GMT from date string.
        String string = "Mon May 14 2010 00:00:00 GMT+0100 (Afr. centrale Ouest)".replace( "GMT" , "" );

        // Parse string to date object.
        Date date = new SimpleDateFormat( "EEE MMM dd yyyy HH:mm:ss Z" ).parse( string );

        // Format date to new format
        System.out.println( new SimpleDateFormat( "MM-dd-yyyy" ).format( date ) );
    }
}

Outputs:

05-13-2010

2 Comments

It's related to the question now, but very brittle.
Updated again, this time with less brittle.

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.