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)
entpnerd
  • 10,049
  • 8
  • 47
  • 68
Meow
  • 18,371
  • 52
  • 136
  • 180
  • 2
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque May 31 '17 at 17:59
  • For anyone visiting this question in 2019 or later I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `ZonedDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). I am immodest enough to recommend [my own answer here](https://stackoverflow.com/a/56374732/5772882). – Ole V.V. Oct 23 '19 at 16:54

6 Answers6

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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. – Meow Dec 21 '10 at 05:00
  • 1
    Runs fine here (after removing `String` from your 2nd code line). Your problem lies somewhere else. – BalusC Dec 21 '10 at 05:02
  • hmm right, I'll remove the "string", but still not working on my local, looking at other places now.. interesting.. – Meow Dec 21 '10 at 05:03
  • 4
    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) – BalusC Dec 21 '10 at 05:06
  • I had "throws ParseException next to the method declaration line and after I removed it and change to try/catch block, it worked. But your example has "throws Exception" too.. mystery. – Meow Dec 21 '10 at 05:07
  • The date string value came from database which was stored in HTML input field and I picked it up and wanting to convert to Date type for column update. I kind of feeling wrong but would appriciate if there is better way. – Meow Dec 21 '10 at 05:09
  • @BalusC: remove the "throws Exception" from method line and change to try/catch. My local test shows that try/catch works but your version still throws an error. (though I'm not sure why.. gotta check exception again) – Meow Dec 21 '10 at 05:14
  • 3
    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`. – BalusC Dec 21 '10 at 05:16
  • You are right, I should not use pure toString output for form input, cuz i'm having super tough time handling it. Maybe convert to MM/dd/yyyy hh:mm:ss would be much friendlier. – Meow Dec 21 '10 at 05:35
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
miku
  • 181,842
  • 47
  • 306
  • 310
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?

Fernando Carvalho
  • 393
  • 1
  • 3
  • 14
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

  • You are right, my IDE which is NetBean3.9 and run the exactly same code throws an error while online IDE does not. – Meow Dec 21 '10 at 05:32
  • I had also same problem. I know this feeling :) correct code but wrong output. update your jdk there will not any problem –  Dec 21 '10 at 05:36
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"

prayagupa
  • 30,204
  • 14
  • 155
  • 192
dennis
  • 146
  • 3
  • 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" – OneWorld Jul 26 '12 at 21:11
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...

nckbrz
  • 688
  • 1
  • 6
  • 20