122

How do I parse the date string below into a Date object?

String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result =  df.parse(target);  

Throws exception...

java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
        at java.text.DateFormat.parse(DateFormat.java:337)
2

6 Answers 6

177

The pattern is wrong. You have a 3-letter day abbreviation, so it must be EEE. You have a 3-letter month abbreviation, so it must be MMM. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify the SimpleDateFormat locale to English as well, otherwise it will use the platform default locale which may not be English per se.

public static void main(String[] args) throws Exception {
    String target = "Thu Sep 28 20:29:30 JST 2000";
    DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy", Locale.ENGLISH);
    Date result =  df.parse(target);  
    System.out.println(result);
}

This prints here

Thu Sep 28 07:29:30 BOT 2000

which is correct as per my timezone.

I would also reconsider if you wouldn't rather like to use HH instead of kk. Read the javadoc for details about valid patterns.

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

9 Comments

Nope, still throwing same error, I've tried with zzz too and no luck. I'm going to check the doc now, thanks for the link.
Runs fine here (after removing String from your 2nd code line). Your problem lies somewhere else.
hmm right, I'll remove the "string", but still not working on my local, looking at other places now.. interesting..
By the way, where does this date string come from? It look very much like the default outcome of java.util.Date#toString(). Aren't you doing some things wrongly? (passing dates around as strings instead of dates)
Works fine here. It's legitimately valid. You have to be more clear about that "an error". As to passing the value around, I'd rather use a fixed and same pattern for formatting (displaying/editing in HTML) and parsing (processing request parameter). E.g. yyyy-MM-dd HH:mm:ss.
|
15

Here is a working example:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Date;

public class j4496359 {
    public static void main(String[] args) {
        try {
            String target = "Thu Sep 28 20:29:30 JST 2000";
            DateFormat df = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyyy");
            Date result =  df.parse(target);
            System.out.println(result); 
        } catch (ParseException pe) {
            pe.printStackTrace();
        }
    }
}

Will print:

Thu Sep 28 13:29:30 CEST 2000

2 Comments

I've tried that already.. it didn't work :(
Updated my post with tested code ;)
13
String target = "27-09-1991 20:29:30";
DateFormat df = new SimpleDateFormat("dd MM yyyy HH:mm:ss");
Date result =  df.parse(target);
System.out.println(result); 

This works fine?

1 Comment

I think you meant: "dd-MM-yyyy HH:mm:ss"
7
new SimpleDateFormat("EEE MMM dd kk:mm:ss ZZZ yyyy");

and

new SimpleDateFormat("EEE MMM dd kk:mm:ss Z yyyy");

still runs. However, if your code throws an exception it is because your tool or jdk or any other reason. Because I got same error in my IDE but please check these http://ideone.com/Y2cRr (online ide) with ZZZ and with Z

output is : Thu Sep 28 11:29:30 GMT 2000

2 Comments

You are right, my IDE which is NetBean3.9 and run the exactly same code throws an error while online IDE does not.
I had also same problem. I know this feeling :) correct code but wrong output. update your jdk there will not any problem
4

I had this issue, and I set the Locale to US, then it work.

static DateFormat visitTimeFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);

for String "Sun Jul 08 00:06:30 UTC 2012"

1 Comment

I had to set the Locale.US also for new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US) to parse"Wed Jan 25 11:13:11 +0100 2012"
0

A parse exception is a checked exception, so you must catch it with a try-catch when working with parsing Strings to Dates, as @miku suggested...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.