Reputation:
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());
}
Upvotes: 0
Views: 13781
Reputation: 79425
There are two problems with your code:
Y
(which specifies Week year) instead of y
(which specifies Year). Check the documentation to learn more about symbols. Learn more about it here.Locale
. Date-time parsing/formatting types are Locale
-sensitive. Learn more about here.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.
Upvotes: 0
Reputation: 16039
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.
Upvotes: 1
Reputation: 159844
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");
Upvotes: 1
Reputation: 10161
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");
Upvotes: 0