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

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)

Related

Unable to parse some dates into ZonedDateTime [duplicate]

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);

Java SimpleDateFormat can not parse date in "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX" format pattern [duplicate]

This question already has answers here:
String-Date conversion with nanoseconds
(5 answers)
StringDate to Date coming in different Time in SimpleDateFormat in java
(2 answers)
Illegal pattern character 'T' when parsing a date string to java.util.Date
(4 answers)
Closed 1 year ago.
I have simple code, which parses string into date.
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSX").parse("2021-06-28T07:09:30.463931900Z")
It parses this string into Sat Jul 03 18:01:41 CEST 2021, which is not valid.
When I remove from pattern 'SSSSSSSSSX', it starts working and returns: Mon Jun 28 07:09:30 CEST 2021.
Problem is that I need nanoseconds, so I can not just get rid of this.
I found many similar topics, but any of them dealt with such date format.
Use java.time:
You can parse this example String without any explicit pattern, keep the precision as desired and, if necessary, format those date and time values in a multitude of custom ways.
Here's a small example:
public static void main(String[] args) {
// example String (of ISO format)
String input = "2021-06-28T07:09:30.463931900Z";
// parse it (using a standard format implicitly)
OffsetDateTime odt = OffsetDateTime.parse(input);
// print the result
System.out.println(odt);
// if you want a different output, define a formatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(
// use a desired pattern
"EEE MMM dd HH:mm:ss O uuuu",
// and a desired locale (important for names)
Locale.ENGLISH);
// print that
System.out.println(odt.format(dtf));
}
This code example produces the following output:
2021-06-28T07:09:30.463931900Z
Mon Jun 28 07:09:30 GMT 2021

Java convert string to date not converting correctly [duplicate]

This question already has answers here:
Unable to parse date Apr 15, 2020 12:14:17 AM to LocalDatetime
(2 answers)
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
simpledate formater to format returns incorrect.deducted date after formating [duplicate]
(2 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 2 years ago.
I have the following:
Date dateCommence = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").parse("2021-01-06 00:00:00");
But the dateCommence is the following:
Sun Dec 27 00:00:00 SAST 2020
Question
How do I convert the "2021-01-06 00:00:00" string to a date?
You were using the wrong date format mask. From the documentation, Y corresponds to the week year, and D is the day in year.
Try this version:
Date dateCommence = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2021-01-06 00:00:00");
System.out.println(dateCommence);
This prints:
Wed Jan 06 00:00:00 CET 2021

SimpleDateFormat Japanese time [duplicate]

This question already has answers here:
Java Date() giving the wrong date [duplicate]
(9 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Invalid date is populated when we use yyyy-MM-dd'T'HH:mm:ssXXX format in java [duplicate]
(1 answer)
Closed 2 years ago.
The time I have is -> September 15 2020 11:10:25
I am using this code to format it in Japanese
timeFormatStr = "YYYY MMMMMMMMMM DD HH:mm:ss z";
SimpleDateFormat sd = new SimpleDateFormat(timeFormatStr, locale);
timeStr = sdf.format(new Date(time));
The timeStr looks like this (does not look right).
2020 9月 259 23:10:25 UTC
Any idea what the format string should be? I checked that the locale is - ja_JP.eucjp
Thanks
YYYY MMMMMMMMMM DD HH:mm:ss z is not how the Japanese format their dates and times. You should use DateTimeFormatter, and call ofLocalizedDateTime and withLocale. This will produce a formatter that produces strings in a native Japanese format.
String formatted = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.FULL) // choose a style here
.withLocale(Locale.JAPANESE)
.format(new Date(time).toInstant().atZone(ZoneOffset.UTC)); // choose a timezone here
System.out.println(formatted); // 1970年1月1日木曜日 0時00分00秒 Z
You shouldn't really be using Dates anymore. You should instead give the DateTimeFormatter a ZonedDateTime directly.

Java: format date string "150520" into "May 20, 2015" [duplicate]

This question already has answers here:
How to change the date format from YYMMDD to YYYY-MM-DD in java? [closed]
(2 answers)
Closed 5 years ago.
I'm using Java 8 on linux with the following code
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
LocalDate exampleDate = LocalDate.parse(myDate, formatter);
where myDate is a String equal to "150520". I'm getting error:
java.time.format.DateTimeParseException: Text '150520' could not
be parsed: Conflict found: Field MonthOfYear 1 differs from
MonthOfYear 5 derived from 2015-01-20
I'd like to return May 20, 2015 for example. Any idea what's wrong?
UPDATE
Replacing the date code D with d eliminates this error. How to format into readable date?
You want yyMMdd. The uppercase D parses "day of year" not "day of month".
[Edit] for the printing part, you could do DateTimeFormatter.ofPattern("MMM dd, yyyy").format(theDate).
Change
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMDD",Locale.ENGLISH);
with
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd",Locale.ENGLISH);
You need to use this format string "yyMMdd". According to the docs
Symbol Meaning Presentation Examples
------ ------- ------------ -------
D day-of-year number 189
d day-of-month number 10

Categories