0

I have tried the below code to parse the date in Tue Apr 11 00:00:00 CEST 2017 format. But, it is showing parsing exception. How can I parse the date format for Tue Apr 11 00:00:00 CEST 2017?

SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
Date bbDate=sdf.parse(String.valueOf("Tue Apr 11 00:00:00 CEST 2017"));
System.out.println(bbDate);
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
M.Nehru
  • 31
  • 2
  • 7

4 Answers4

1

You have an unhandled exception. You need to surround the parse line in a try-catch, or have the function itself throw an exception.

    SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");

    try {
        Date bbDate = sdf.parse("Tue Apr 11 00:00:00 CEST 2017");
        System.out.println(bbDate);
    } catch (ParseException e) {
        // … Handle exception.
        System.out.println( "ERROR…" );
    }

Here is the documentation to learn more about Java exceptions. https://docs.oracle.com/javase/tutorial/essential/exceptions/

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Robert
  • 287
  • 1
  • 9
  • 1
    Please, not even in a code snippet meant to explain something, or maybe particularly not here, *don’t* swallow an exception. – Ole V.V. Apr 19 '17 at 18:58
1

It is better to use new Java Date Time api: http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html

You can do the same thing with the new api as follows:

ZonedDateTime.parse("Tue Apr 11 00:00:00 CEST 2017",
            DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy")).toLocalDateTime();
Ali Sağlam
  • 1,410
  • 1
  • 10
  • 15
0

Its just not handled Parse Exception. Your SimpleDateFormat is good.

try {
        SimpleDateFormat sdf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
        Date bbDate;
        bbDate = sdf.parse(String.valueOf("Tue Apr 11 00:00:00 CEST 2017"));
        System.out.println(bbDate);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
strash
  • 1,291
  • 2
  • 15
  • 29
  • 1
    Well. I tried even with handling exceptions. Still no luck.I am still getting the same exception.Does this to do with the java version as I am using Java Version 6(1.6.0.29)? – M.Nehru Apr 19 '17 at 09:48
  • yes maybe it is from the java version. try switching to java 7 or java 8 – strash Apr 19 '17 at 09:52
  • they changed simpledateformat in java 7 i think – strash Apr 19 '17 at 09:53
  • In My application, We are using java 6 only. So, I need to parse the date in Java 6 format. – M.Nehru Apr 19 '17 at 10:22
0

Since your string uses English names for week day and month, you need to make sure that your SimpleDateFormat uses a locale with English language, for example:

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

With this change your code prints (tested with Java 8):

Tue Apr 11 00:00:00 CEST 2017

EDIT: please fill in the appropriate (English-speaking) locale instead of Locale.ROOT.

I agree with Ali Sağlam in warmly recommending the java.time classes introduced in Java 8. You will still need to control the locale, though:

    ZonedDateTime.parse("Tue Apr 11 00:00:00 CEST 2017",
                        DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy",
                                                    Locale.ROOT))
            .toLocalDateTime();

Finally, while we’re at the recommendations, see if you can find a way to avoid the three and four letter time zone abbreviations like CEST. Many of them are ambiguous, and CEST isn’t even really a full time zone, just the summer half of the Central European time zone. While it works as expected with a date in the summer time half of year, I’m not even sure what I should expect if one day you have a date in winter with CEST time zone abbreviation.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • `Locale.ROOT` ?? – Basil Bourque Apr 19 '17 at 19:32
  • Interesting question, @BasilBourque. I am routinely using `Locale.ROOT` to mean something like “the locale neutral locale”. For example, rather than passing a `null` for locale to `String.format()` I prefer to pass `Locale.ROOT` to mean “do not apply any localization”. In this case I was in doubt, but figured that the input String more likely came out of either `Date.toString()` or a corresponding non-locale-dependent source in some other system. If the string is in English because it comes from an English speaking locality, one should of course use the corresponding locale, not `ROOT`. – Ole V.V. Apr 19 '17 at 22:11