Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse a String to Date in Java

I'm trying to parse a string to a date, this is what I have:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zZ (zzzz)");
Date date = new Date();
try {
    date = sdf.parse(time);
} catch (ParseException e) {
    e.printStackTrace();
}

the string to parse is this:

Sun Jul 15 2012 12:22:00 GMT+0300 (FLE Daylight Time)

I followed the http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Pretty sure I've done everything by the book. But it is giving me ParseException.

java.text.ParseException: Unparseable date: 
"Sun Jul 15 2012 12:22:00 GMT+0300 (FLE Daylight Time)"

What am I doing wrong? Patterns I Have tried:

EEE MMM dd yyyy HH:mm:ss zzz
EEE MMM dd yyyy HH:mm:ss zZ (zzzz)
like image 746
Jaanus Avatar asked Jul 12 '12 06:07

Jaanus


2 Answers

You seem to be mixing the patterns for z and Z. If you ignore the (FLE Daylight Time), since this is the same info as in GMT+0300, the problem becomes that SimpleDateFormat wants either GMT +0300 or GMT+03:00. The last variant can be parsed like this:

String time = "Sun Jul 15 2012 12:22:00 GMT+03:00 (FLE Daylight Time)";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz");
Date date = sdf.parse(time);

[EDIT]
In light of the other posts about their time strings working, this is probably because your time string contains conflicting information or mixed formats.

like image 99
Keppil Avatar answered Oct 22 '22 22:10

Keppil


java.time

I should like to contribute the modern answer. Use java.time, the modern Java date and time API, for your date and time work.

First define a formatter for parsing:

private static final DateTimeFormatter PARSER = DateTimeFormatter
        .ofPattern("EEE MMM dd yyyy HH:mm:ss 'GMT'Z (", Locale.ROOT);

Then parse in this way:

    String time = "Sun Jul 15 2012 12:22:00 GMT+0300 (FLE Daylight Time)";
    
    TemporalAccessor parsed = PARSER.parse(time, new ParsePosition(0));
    OffsetDateTime dateTime = OffsetDateTime.from(parsed);
    
    System.out.println(dateTime);

Output is:

2012-07-15T12:22+03:00

I am not parsing your entire string, but enough to establish a point in time and an offset from GMT (or UTC). Java cannot parse the time zone name FLE Daylight Time. This is a Microsoft invention that Java does not know. So I parse up to the round bracket before FLE in order to validate this much of the string. To instruct the DateTimeFormatter that it needs not parse the entire string I use the overloaded parse method that takes a ParsePosition as second argument.

From Wikipedia:

Sometimes, due to its use on Microsoft Windows, FLE Standard Time (for Finland, Lithuania, Estonia, or sometimes Finland, Latvia, Estonia) … are used to refer to Eastern European Time.

If you indispensably need a Date object, typically for a legacy API that you cannot afford to upgrade to java.time just now, convert like this:

    Date oldfashionedDate = Date.from(dateTime.toInstant());
    System.out.println(oldfashionedDate);

Output when run in Europe/Tallinn time zone:

Sun Jul 15 12:22:00 EEST 2012

What went wrong in your code?

Your SimpleDateFormat successfully parsed GMT+03 into a “time zone” matching the small z in the format pattern string. It then tried to parse the remaining 00 into an offset to match the capital Z. Since an offset requires a sign, this failed.

What am I doing wrong?

As others have said, you should not try to parse GMT into a time zone abbreviation. GMT can be used as a time zone abbreviation; but your time is not in GMT. So you don’t want that. It would only be misleading. Had you been successful, you would rather have risked an incorrect result because you had parsed a time zone that was incorrect for your purpose.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Eastern European Time on Wikipedia.
like image 2
Ole V.V. Avatar answered Oct 22 '22 23:10

Ole V.V.