3

String that I want to parse:

Sun Nov 10 10:00:00 CET 2013

My code:

substrings = "Sun Nov 10 10:00:00 CET 2013"

SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZ yyyy");
Date date = parserSDF.parse(substrings);

Compiler error output:

java.text.ParseException: Unparseable date: "Sun Nov 10 10:00:00 CET 2013" at java.text.DateFormat.parse(Unknown Source) ...

Facon
  • 679
  • 2
  • 7
  • 21

4 Answers4

1

Probably you are missing the correct Locale. Try this, in your example:

    SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZ yyyy", Locale.US);
Italo Borssatto
  • 15,044
  • 7
  • 62
  • 88
0

ZZZ will attempt to format the time zone in accordance to RFC 822 time zone, use zzz instead.

Here's the JavaDoc link for reference

Fritz
  • 9,987
  • 4
  • 30
  • 49
LCartwright
  • 356
  • 1
  • 5
  • 12
0

java.time

The modern approach uses the java.time classes.

String input = "Sun Nov 10 10:00:00 CET 2013";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" , Locale.US );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );

zdt.toString(): 2013-11-10T10:00+01:00[Europe/Paris]

The terrible old java.util.Date class was replaced years ago by the java.time.Instant class. Both represent a moment in UTC. To adjust from the time zone seen above to UTC, simply extract a Instant object.

Instant instant = zdt.toInstant() ;  // Adjust from time zone to UTC.

Avoid java.util.Date. But if you must interoperate with old code not yet updated to java.time, you can convert back and forth. See the new methods added to the old classes, such as Date.from and Date::toInstant methods.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

The problem is that parserSDF takes a string rather than an array and a ParsePosition. I wrote this to output what I think you want.

import java.text.*;
public class Parser{
    public static void main(String[] args){
    String input = "Sun Nov 10 10:00:00 CET 2013";
    SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM d HH:mm:ss ZZZ yyyy");
    System.out.println(parserSDF.parse(input, new ParsePosition(0)));
    }
}

I recommend taking a look at SimpleDateFormat and ParsePosition within the API

ChocPanda
  • 88
  • 6