Unable to parse some dates into ZonedDateTime [duplicate] - java

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 1 year ago.
Trying to parse incoming dates from a third-party source, some dates work as expected others throw an exception:
Text 'Fri, 11 Jun 2021 02:25:23 +0000' could not be parsed at index 8
Looking at the dates I can't spot a difference in them, and looking at my formatter I can't see where I've gone wrong.
Example failing date: Fri, 11 Jun 2021 02:25:23 +0000
Example passing date: Sun, 30 May 2021 11:42:03 +0000
The code I'm using to parse the dates:
ZonedDateTime.parse(incomingDate, DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z"));
The only thing I can think that some date months are shorthand vs others that are not (May vs Jun for example).
Would love some help.

String incomingDate1 = "Fri, 11 Jun 2021 02:25:23 +0000";
String incomingDate2 = "Sun, 30 May 2021 11:42:03 +0000";
ZonedDateTime parsed = ZonedDateTime.parse(incomingDate1,
DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
System.out.println(parsed);

Related

DateTimeFormatter throwing exception "could not be parsed at index 0" [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
DateTimeFormatter month pattern letter "L" fails
(3 answers)
Closed 2 years ago.
I keep on getting an error from running this code.
java.time.format.DateTimeParseException: Text 'Jan 03, 2020' could not be parsed at index 0
final String myFormat = "LLL dd, yyyy"; //sets format in which to show date (same as how its saved in database) ex. Jan 29, 2020
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(myFormat);
String startingBiWeeklyCheck = sharedPreferences.getString("biweekly start", "Jan 03, 2020");
LocalDate startingDate = LocalDate.parse(startingBiWeeklyCheck, dateFormatter);
Ive played around with the format but I'm not seeing why the pattern "LLL dd, yyyy" doesn't parse Jan 03, 2020
You should use MMM instead of LLL for month parsing.
Updated:
I was wrong about my answer above. It's the half of answer.
The deal is DateTimeFormatter.ofPattern(myFormat) uses default Locale.
For non-US locales, it doesn't work.
So you need to specify the locale according to your pattern.
DateTimeFormatter.ofPattern(myFormat).withLocale(Locale.US)

How to parse TZ format time with non-GMT time zone [duplicate]

This question already has answers here:
Parse ISO8601 date string to date with UTC Timezone
(4 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 3 years ago.
I have time string in two formats.
2019-03-01T22:22:50.591-08:00
2019-03-01T22:22:50.591Z
I am using following Java snippet to parse
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS" +
".SSS'Z'", Locale.US);
Date date = df.parse(dateString);
Its working on second string but failing on first. What common code can be there to parse both of them?
This might be what you're looking for: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
Date parsed1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US).parse("2019-03-01T22:22:50.591Z");
System.out.println(parsed1); // Fri Mar 01 16:22:50 CST 2019
Date parsed2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US).parse("2019-03-01T22:22:50.591-08:00");
System.out.println(parsed2); // Sat Mar 02 00:22:50 CST 2019
As Basil points out below, these also work if you are running Java 8+ or a backport of JSR 310:
OffsetDateTime parsed1 = OffsetDateTime.parse("2019-03-01T22:22:50.591Z");
System.out.println(parsed1); // Fri Mar 01 16:22:50 CST 2019
OffsetDateTime parsed2 = OffsetDateTime.parse("2019-03-01T22:22:50.591-08:00");
System.out.println(parsed2); // Sat Mar 02 00:22:50 CST 2019

Convert into format YYYY-MM-DD [duplicate]

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I want this gmt time into YYYY-MM-DD format but i went through the procedure it give parse exception error.
I want to convert this String type of Fri Apr 08 2016 00:00:00 GMT+0530 (IST) into date format so that i can insert into my mqysql data base.
If you want to get "YYYY-MM-DD" formate of mysql date format this code could help.
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("YYYY-MM-dd");
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("EEE MMM dd yyyy hh:mm:ss zZ (z)");
TemporalAccessor parse = formatter.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)");
LocalDate localDate=LocalDate.from(parse);
String formated= dateTimeFormatter.format(localDate);
System.out.println("formated:"+formated);

java.util.Date.parse() throws an exception when parsing Wed Apr 03 00:00:00 BST 2013

Why is it throwing an exception, That date is pretty straight forward isnt it?
long date = Date.parse(request.getParameter("date")); //Wed Apr 03 00:00:00 BST 2013
String formattedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
reportParams.put("p_date", formattedDate);
Caused by: java.lang.IllegalArgumentException at
java.util.Date.parse(Date.java:595)
Don't use Date.parse() to parse dates. As you can see in the API documentation, that method is deprecated, which means it is replaced by another method. The API documentation even mentions what you should use instead: DateFormat.parse().
Create a SimpleDateFormat object with the format that matches your input string, and use that to parse it into a Date object.
String text = "Wed Apr 03 00:00:00 BST 2013";
DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
Date date = df.parse(text);
The main issue is that you have the date at the end of the string. It should come after the month, e.g:
Wed, 03 Apr 2013 00:00:00 BST
Please read the documentation for a full description. Note also that Date.parse is deprecated in favour of DateFormat.parse.
1) Date.parse is deprecated
2) Date.parse API says It accepts many syntaxes; in particular, it recognizes the IETF standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also understands the continental U.S. time-zone abbreviations, but for general use, a time-zone offset should be used: "Sat, 12 Aug 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If no time zone is specified, the local time zone is assumed. GMT and UTC are considered equivalent. But your syntax is none of the described.
3) Use SimleDateFormat instead

"Unparseable date" using SimpleDateFormatter with API code example

I am trying to parse dates, for example "Sat, 29 Dec 2012 04:07:09 +0100"
I am using SimpleDateFormatter with format set as
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.getDefault());
Unfortunately I am getting
12-29 04:44:29.890: E/MainActivity(3995): Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)
12-29 04:44:29.890: E/MainActivity(3995): java.text.ParseException: Unparseable date: "Sat, 29 Dec 2012 04:07:09 +0100" (at offset 0)
Which I absolutely dont get as that parameter regular expression is copy and pasted directly from documentation http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html under Examples
Thanks!
Whatever the locale returned by Locale.getDefault() is doesn't support dates formatted that way.
Changing it to Locale.US for example, will work.

Categories