18

I got problem with date parse example date:

SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());


parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

got exception

Exacly I want parse this format date to yyyy-MM-dd I try:

SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");

take : java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013"


OK I change to and works :

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
PDS
  • 562
  • 3
  • 13
  • 27
  • ParseException I guess – Shoaib Chikate Nov 08 '13 at 14:40
  • I run your code.there is no exception..please attach exception stack trace.. – jdev Nov 08 '13 at 14:56
  • 2
    FYI, the terribly troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 16 '18 at 03:57

3 Answers3

24

I'm going to assume that Locale.getDefault() for you is pl-PL since you seem to be in Poland.

English words in date strings therefore cause an unparseable date.

An appropriate Polish date String would be something like

"Wt paź 16 00:00:00 -0500 2013"

Otherwise, change your Locale to Locale.ENGLISH so that the SimpleDateFormat object can parse String dates with English words.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
10

Instead of using Locale.default that you and others often don't know which default, you can decide by using locale.ENGLISH because I see your string date is format in English. If you are at other countries, the format will be different.

Here is my example code:

public static void main(String[] args) {
    try {
        SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
        Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
        System.out.println("date: " + date.toString());
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

The result will be : date: Wed Oct 16 05:00:00 ICT 2013. Or you can decide which part of this date to be printed, by using its fields.

Hope this help :)

hqt
  • 29,632
  • 51
  • 171
  • 250
6

I think the original Exception is due to Z in your format. Per documentation:

Z   Time zone   RFC 822 time zone   -0800

most likely you meant to use lower case z

Алексей
  • 1,847
  • 12
  • 15