SimpleDateFormat: unparseable date exception with correct pattern - java

I have simple code, it still throws me unparseable date exception. I'm confused, the pattern seems to be correct.
DateFormat dffrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
dffrom.parse("Sun Jan 20 00:50:24 CET 2013");
I tried also these patterns:
"EEE MMM dd HH:mm:ss z yyyy"
"EEE MMM dd HH:mm:ss z YYYY"
"EEE MMM dd HH:mm:ss zzz YYYY"

Your default Locale may not recognize the words "Sun" and/or "Jan". Try with ENGLISH Locale:
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
To view your default Locale you can use:
System.out.println(Locale.getDefault());
See: Locale

This depends on your Locale.
DateFormat dffrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
will work.

Related

Unparseable date error

I have this
java.text.ParseException: Unparseable date: "Thu, 21 Apr 2016 18:00:00
+0000" (at offset 26)
when using new SimpleDateFormat("E, dd MMMM yyyy hh:mm:ss a", Locale.ROOT);
Why is that can be?
EDIT:
This is correct answer due to parsing pattern.
SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);
And also Android somehow gives error when the Locale is ROOT and when it is ENGLISH everything works fine.
You are using the wrong format. You should be using EEE, dd MMM yyyy HH:mm:ss Z instead of E, dd MMMM yyyy hh:mm:ss a.
Here is the code snippet:
public static void main (String[] args) throws Exception
{
String foo = "Thu, 21 Apr 2016 18:00:00 +0000";
SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ROOT);
System.out.println(f.parse(foo));
}
Output:
Thu Apr 21 18:00:00 GMT 2016
Use EEE, dd MMM yyyy HH:mm:ss Z

Joda DateTimeFormatter Invalid format

I am getting the above error, but to me everything seems to be correct.
What I am doing wrong?
DateTimeFormatter simpleDateFormatInput= DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z");
DateTime datetime = simpleDateFormatInput.parseDateTime(pubDate);
Where pubDate is Sat, 30 Jan 2016 12:23:53 +0100
The day and/or month from your input String may not match those from your default Locale. Try
DateTimeFormatter simpleDateFormatInput =
DateTimeFormat.forPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);

Java- Parsing Twitter's created_at String

So I call this method:
System.out.println(getTwitterDate("Thu, 3 Jan 2010 18:26:07 +0000").getMonth());
And I keep getting 11 for every month I use.
I'm doing this in the getTwitterDate:
final String TWITTER="EEE, dd MMM YYYY HH:mm:ss ZZZZZ";
SimpleDateFormat sf = new SimpleDateFormat(TWITTER);
return sf.parse(date);
I'm going by this https://stackoverflow.com/a/4522095/1193534 but removed the sf.lenient part.
What am I doing wrong?
Just use small y instead of Y:
final String TWITTER="EEE, dd MMM yyyy HH:mm:ss ZZZZZ";
This is the latest created_at format received from the Twitter response for GET lists/statuses.
"EEE MMM dd HH:mm:ss ZZZZZ yyyy"

Timezone conversion (GMT to GMT)

I have dates in following formats
EEEE MMM dd, yyyy HH:mm aaa
EEE, dd MMM yyyy HH:mm:ss z
EEE, MMMM dd, yyyy, HH:mm aaa z
yyyy-MM-dd-HH-mm
yyyyMMddHHmm
EEEE, MMMM dd, yyyy HH:mm aa
yyyyMMddHHmmssSS
yyyyMMddHHmmss
MM/dd/yyyy HH:mm
I want these dates to be in my local GMT, and convert from one GMT to other GMT dynamically in java.
Is there any native java function to this or i have to code it.
e.g convert 11/07/2013 12:32pm (MM/dd/yyyy HH:mm) to GMT+5 timezone
i mean to say that if my datetime is in GMT-10 timezone, that what will be its value in GMT+5 timezone
Any help will be appreciated. Thanks
private String converttogmt(Date date){
SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
datef.setTimeZone(TimeZone.getTimeZone("GMT"));
return datef.format(date);
}
Try this one.

Parse date in android

I have the following code to pase a date
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
Date _pubDate = df.parse(_pubDateE.getFirstChild().getNodeValue());
But I get this error:
java.text.ParseException: Unparseable date: "Fri, 12 Aug 2011 15:34:47 CEST"
What is wrong ?
You're missing the timezone in the date format at the end, in your exception message, the "CEST" part.
Your code
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss", Locale.getDefault());
should be
DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss z", Locale.getDefault());
You might want to read SimpleDateFormat
Edit
At the bottom of this page, the timezone format is more cleary explained
Clearer Timezone format
I think you need to add zzz in the end (for timezone):
"EEE, dd MMM yyyy kk:mm:ss zzz"
The date string is not in the format you specified. Notice the time zone at the end?
You probably want: new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss zzz", Locale.getDefault());

Categories