0

I'm getting parser exception on trying to parse string value:

"Thursday, July 27, 2006 10:10:02 PM PST" 

To format:

"EEEE, MMMM d, YYYY h:mm:ss a z"

This is the program sample:

DateTime.parse("Thursday, July 27, 2006 10:10:02 PM PDT", DateTimeFormat.forPattern("EEEE, MMMM d, yyyy h:mm:ss a z"));

And this is the error message:

Invalid format: "Thursday, July 27, 2006 10:10:02 PM PDT" is malformed at "PDT"

this is my sample program

String str = "Thursday, July 27, 2006 10:10:02 PM PDT"; 
DateTimeFormatter formatterDateTime = DateTimeFormat.forPattern("EEEE, MMMM d, YYYY h:mm:ss a z");
try{
    DateTime dt = DateTime.parse(str, formatterDateTime);
}catch(Exception ex)
{
    System.out.println(ex.getMessage());
}
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92

4 Answers4

1

From the JodaTime docs:

Zone names: Time zone names ('z') cannot be parsed.

However SimpleDateFormat does support parsing of timezones.

SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, YYYY h:mm:ss aa zzz");
Date date = format.parse("Thursday, July 27, 2006 10:10:02 PM PST");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

As suggested by marba, the error most likely is caused by using Java 7 specific pattern with a Java 6.

Your code for parsing the date can look like this:

SimpleDateFormat df = new SimpleDateFormat("EEEE, MMMM dd, yyyy h:mm:ss aa zzz");
Date d = df.parse("Thursday, July 27, 2006 10:10:02 PM PST");

To test that the parsed date is the same as the provided date:

df.setTimeZone(TimeZone.getTimeZone("Pacific/Pitcairn"));
System.out.println(df.format(d));

Prints:

Thursday, July 27, 2006 10:10:02 PM PST

Refer to the Javadoc for more patterns.

Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
  • How do you know that `YYYY` is causing the exception? – maba May 15 '13 at 14:05
  • Tried it myself and checked the Javadoc, there is no pattern for `YYYY`. – Adam Siemion May 15 '13 at 14:06
  • In Java 7 it is legal: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – maba May 15 '13 at 14:07
  • And that might be the problem (that he is using Java 6), but we don't know that. – maba May 15 '13 at 14:08
  • Well, if someone has created a question on SO, pasted some code and claims he gets an exception in this code then I think we can assume that the exception actually comes from this code. Also if the OP were not getting an exception then I think he would not ask in the first place. – Adam Siemion May 15 '13 at 14:24
  • You're welcome. If you update your link to point to Java 7 I will give you my +1. And also point out that the error most likely is caused by using Java 7 specific pattern with a Java 6 JRE. – maba May 15 '13 at 15:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30007/discussion-between-adam-siemion-and-maba) – Adam Siemion May 15 '13 at 15:13
  • There's nothing to continue :-) – maba May 15 '13 at 15:17
0

What locale do you use? I think you have to explicitly provide Locale.US as a second parameter to SimpleDateFormat.

For Joda-Time library you can use following code to adjust locale:

DateTimeFormat.forPattern("EEEE, MMMM d, YYYY h:mm:ss a z").withLocale(Locale.US);

Update: Just found this related SO question, looks like you need to use SimpleDateFormat instead. Joda-Time parser does not support time zones:

SimpleDateFormat df = new SimpleDateFormat("EEEE, MMMM d, YYYY h:mm:ss a z");
Date d = df.parse("Thursday, July 27, 2006 10:10:02 PM PDT");
Community
  • 1
  • 1
hoaz
  • 9,883
  • 4
  • 42
  • 53
0

There are two problems with your code:

  1. You have used Y (which specifies Week year) instead of y (which specifies Year). Check the documentation to learn more about symbols. Learn more about it here.
  2. Your date-time string is in English and therefore your code won't work in an expected manner if you run it on a JVM with non-English Locale. Date-time parsing/formatting types are Locale-sensitive. Learn more about here.

java.time

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using the modern API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, MMMM d, u h:m:s a z", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse("Thursday, July 27, 2006 10:10:02 PM PST", dtf);
        System.out.println(zdt);
    }
}

Output:

2006-07-27T22:10:02-07:00[America/Los_Angeles]

In case you need to convert this object of ZonedDateTime to an object of java.util.Date, you can do so as follows:

java.util.Date date = Date.from(zdt.toInstant());

Learn more about the the modern date-time API* from Trail: Date Time.

Solution using the legacy API:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM d, y h:m:s a z", Locale.ENGLISH);
        Date date = sdf.parse("Thursday, July 27, 2006 10:10:02 PM PST");
        //...
    }
}

* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110