Cannot parse date time, issue with parsing pattern "ssSSS" [duplicate] - java

This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 3 years ago.
So I am trying to parse:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438514");
And I receive the following error:
"Text '20180521073438514' could not be parsed at index 0"
But when I try to add sth that will separate "ss" and "SSS" it works:
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS");
TemporalAccessor parse = dateTimeFormatter.parse("20180521073438.514");
Am I missing some kind of delimiter to separate "ss" and "SSS"?

This is a bug in Java 8 which was fixed in Java 9. Have a look at the official bug report https://bugs.openjdk.java.net/browse/JDK-8031085

Related

Parse date "2020-05-22T12:51:20.765111Z" to Instant in Java [duplicate]

This question already has answers here:
How to validate the DateTime string format "2018-01-22T18:23:00.000Z" in Java?
(2 answers)
Closed 2 years ago.
How to parse "2020-05-22T12:51:20.732111Z" to Instant in Java?
I used:
LocalDateTime.parse(
startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US))
.atZone(ZoneId.of("America/Toronto"))
.toInstant()
but with error:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-05-22T12:51:20.732111Z' could not be parsed at index 24
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at Instant.Main.main(Main.java:54)
Use Instant#parse(String):
Instant i = Instant.parse("2020-05-22T12:51:20.732111Z");
And if you want to convert it to your timezone:
ZonedDateTime z = i.atZone(ZoneId.of("America/Toronto"));
Printing it yields:
2020-05-22T08:51:20.732111-04:00[America/Toronto]

Java datetime parse exception: text cannot be parsed [duplicate]

This question already has an answer here:
Exception when trying to parse a LocalDateTime
(1 answer)
Closed 2 years ago.
This piece of code is working fine on my old computer. However, once I moved it to this computer , i received this exception. I looked into the possible answers from other posts, i.e. adding Locale.US do not work.
Basically,
Locale locale = new Locale("en", "US");
DateTimeFormatter dtformatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS",locale);
LocalTime time = LocalTime.parse("20190502050634678",dtformatter);
Exception in thread "main" java.time.format.DateTimeParseException: Text '20190502050634678' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(Unknown Source)
at java.time.format.DateTimeFormatter.parse(Unknown Source)
at java.time.LocalTime.parse(Unknown Source)
I am not sure how do I overcome this ? I removed Locale and that do not work either.
This is because Java failed to parse the fraction of a second part. There is a bug in the jdk thats not fixed until Java 9. Probably that's why it fails when you move the program from one to another computer, because they used different Java runtime. You can update the runtime on your other computer to Java 9.
Or use the following code.
Locale locale = new Locale("en", "US");
DateTimeFormatter formatter =
new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
LocalTime time = LocalTime.parse("20190502050634678",formatter);
Solution is borrowed from here Is java.time failing to parse fraction-of-second? .

Unparseable date exception (1980-02-22T00:00:00Z) [duplicate]

This question already has answers here:
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
ISO 8601 String to Date/Time object in Android
(5 answers)
Closed 4 years ago.
I'm trying to parse 1980-02-22T00:00:00Z into "java.util.Date" using
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'").parse(stringdate)
But I got error
caused by: java.text.ParseException: Unparseable date.
How to parse String like this into Date to get time in milliseconds? I've tried to use SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'") and SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
Just replace:
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
by:
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
If you can use java8, you can use LocalDateTime class,
you can do below:
As per suggestions below, I have corrected my code to parse the date.
String text = "1980-02-22T12:10:02Z";
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.parse(text),ZoneOffset.UTC);
System.out.println(dateTime);
Result:
1980-02-22T12:10:02

Date time parse exception in previously working code [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 4 years ago.
I have this code fragment which worked previously for half year ( I wrote it myself ).
Yesterday I received new laptop with windows 10 ( previously 8.1 ) installed the most recent Java JDK jdk1.8.0_181 and this code stopped working with error.
Is it something I was missing for the whole time or there were some changes in java internal API ?
How I can fix it ? I believe it was written properly.
Caused by: java.time.format.DateTimeParseException: Text '29-Apr-2010,13:00:14' could not be parsed at index 3
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss");
private static final LocalDate DATE = LocalDate.parse("29-Apr-2010,13:00:14", PP_FORMATTER);
try to add Locale.US
private static final DateTimeFormatter PP_FORMATTER = DateTimeFormatter.ofPattern("d-MMM-yyyy,HH:mm:ss", Locale.US);
otherwise, you maybe able to parse only numeric format for the month.

how to convert "2012-04-30" date into "04-Apr-2012" format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formatting Dates using a Format class (or otherwise if )
I am using "My Sql" Db, In my DB table,i am storing Date in "2012-04-30" format but now i have to show it in my jsp page in this "04-Apr-2012" format.
How can i do it with Java Code?
Date d = (new SimpleDateFormat("yyyy-MM-dd").parse("2012-04-30"));
String result = (new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH).format(d));
System.out.println(result);

Categories