java.text.ParseException - With right format [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
How to Convert RFC-1123 date-time formatter, to local time
(1 answer)
Closed 4 years ago.
I'm trying to parse this date
Thu, 15 Nov 2018 16:56:49 +0000
With this code:
Date date = null;
try {
SimpleDateFormat parser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date = parser.parse(xmlPullParser.nextText());
} catch (ParseException e) {
e.printStackTrace();
date = new Date(); //This is just a temporary workaround
}
java.text.ParseException: Unparseable date: "Thu, 15 Nov 2018 16:56:49
+0000"
I've already try this formats too
EEE, dd MMM yyyy HH:mm:ss sssZ
EEE, d MMM yyyy HH:mm:ss sssZ
EEE, d MMM yyyy HH:mm:ss Z
But obviously it doesn't work

You forgot to set the Locale of SimpleDateFormat. Since you try to read a date in a english form, I would initialize this way:
SimpleDateFormat parser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
When you don't specify any Locale, it uses the Locale of your system which is obviously not US or UK.

Related

Need help parsing String into date [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical
(4 answers)
Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]
(3 answers)
Closed 3 years ago.
So I get this String ("Tue Nov 26 12:05:19 CET 2019") from a txt fiel and I want to parse it into a Date like this:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = null;
try {
date = format.parse(dateAsString);
} catch (ParseException e) {
e.printStackTrace();
}
And I still get this Exception:
java.text.ParseException: Unparseable date: "Tue Nov 26 12:05:19 CET 2019"
But the format/patter should be ok. So my question is how I parse the string into a date.
You are most likely not using English locale so Tue and Nov are not parsing. Specify the locale with the formatter and don't use obsolete date classes:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
ZonedDateTime time = ZonedDateTime.parse("Tue Nov 26 12:05:19 CET 2019", fmt);
System.out.println(time); // 2019-11-26T12:05:19+01:00[Europe/Paris]
you can set language as English here
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date date = null;
try {
date = format.parse("Tue Nov 26 12:05:19 CET 2019");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
This worked for me:
SimpleDateFormat format =
new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy", new Locale("en", "EN"));

How to Parse Date in java [duplicate]

This question already has answers here:
How can I convert Date.toString back to Date?
(5 answers)
Closed 4 years ago.
Unparseable date: "Tue Jul 03 16:59:51 IST 2018" Exception
String date="Tue Jul 03 16:59:51 IST 2018";
i want to parse it.
My code is
SimpleDateFormat newformat=SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
Date d=newformat.parse(date);
You are using old and quite problematic classes, especially Date.
For your example, perhaps consider using LocalDateTime
String date = "Tue Jul 03 16:59:51 IST 2018";
DateTimeFormatter format = DateTimeFormatter
.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(date, format);
Since your pattern has to match the string you want to parse you need to adjust the pattern as following:
EEE MMM dd HH:mm:ss z yyyy
Infos gathered from the docs.
You should also use the new java.time API introduced with Java 8.
String s = "Tue Jul 03 16:59:51 IST 2018";
//Java 7 way
SimpleDateFormat newformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = newformat.parse(s);
//Java 8 way
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(s, formatter);

Convert String to Date error [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 7 years ago.
I try to convert string to date format, but the following does't work.
String stringdate = "Fri Mar 27 17:14:27 EET 2015";
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
Date newdate = format.parse(stringdate);
} catch (ParseException e) {
e.printStackTrace();
}
the output is
java.text.ParseException: Unparseable date: "Fri Mar 27 17:14:27 EET
2015"
SimpleDateFormat is locale-sensitive, so your default locale may be the reason why the exception is thrown. Try setting the locale to US.
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

Read date from file using java [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 8 years ago.
I'm triyng to make a program read a date from a txt file into a Date object. Here is the code I'm using :
reader = new FileReader(fich);
rd = new Scanner(reader);
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
Date tmpdate= formatter.parse(rd.nextLine());
And this is the line I want to read:
Fri Feb 20 01:23:35 GMT 2015
and then it shows up this error:
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Feb 20 01:23:35 GMT 2015"
what is wrong?
It seems that Fri and Feb are not correct names in your locale. Try specifying one where they are correct like Locale.ENGLISH.
So instead of
new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
use
new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);

What is the time format string for this date: Sat, 26 Jul 2014 11:55:55 GMT [duplicate]

This question already has answers here:
Java date format convert on Android
(8 answers)
Closed 8 years ago.
I need to create a SimpleDateFormat in order to parse this date and convert it to another format but I dont know how to pass it into the constructor:
SimpleDateFormat sdf = new SimpleDateFormat("???");
Your date format should be
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH);
See here for more info.
Here is a DateFormat cheat sheet:
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Where
E = Day in week
d = Day in month
M = Month in year
y = Year
H = Hour in day
m = Minute in hour
s = Second in minute
z = Time zone
For more info see SimpleDateFormat
"Sat, 26 Jul 2014 11:55:55 GMT"
"EEE, d MMM yyyy HH:mm:ss z"
sdf1 = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.ENGLISH);

Categories

Resources