0

I found an issue, down't know how to fix and explain it. Is it java bug or what? Why the first example works with milliseconds delimiter:

ZonedDateTime timeAsUTC1 = LocalDateTime.from(DateTimeFormatter.ofPattern("yyyyMMddHHmmss-SSS").parse("20190902165550-091")).atZone(ZoneId.of("UTC"));
System.out.println(timeAsUTC1);

But the second one without delimiter:

ZonedDateTime timeAsUTC2 = LocalDateTime.from(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").parse("20190902165550091")).atZone(ZoneId.of("UTC"));
System.out.println(timeAsUTC2);

fails with the following excpetion:

Exception in thread "main" java.time.format.DateTimeParseException: Text '20190902165550091' could not be parsed at index 0
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1777)

My project db uses datetime as ID so it's is just a number, and i'd like to parse it.

Can't reproduce in ideone: https://ideone.com/XFnYFn I suppose it was fixed after java8 which i'm using

sergentum
  • 193
  • 1
  • 7
  • try ```"yyyy-MM-dd'T'HH:mm:ss.SSS"```? – Paul Jul 16 '20 at 11:05
  • @Paul Just curious is he passing something wrong ? or as he mentioned was it bug before java8? – Vinay Hegde Jul 16 '20 at 11:07
  • @Daemon I'm not an expert myself. But parsing time in Java depends on several factors. If he receives the number as an ID (Long, etc...), then there are several options. If it's stored as a Timestamp, then it'd be better to use java.util.Date and parse according my previous suggestion.To my knowledge it won't be a bug, but parsing-method standard exception concerning requested format. – Paul Jul 16 '20 at 11:12
  • [It works in java 8 (sun-jdk-1.8.0_51)](https://ideone.com/8NCDGr) – Lino Jul 16 '20 at 11:12
  • @Paul The thing is that the code runs just fine in java 9+ but doesn't in java 8. Most likely some bug. – Amongalen Jul 16 '20 at 11:13
  • @sergentum can you [edit] your question with the exact java version you're using? – Lino Jul 16 '20 at 11:14
  • @Paul Somewhere I read using a java.uti.Date is kinda outdated after java8 introduced the date and time library anyway, thanks for your response. – Vinay Hegde Jul 16 '20 at 11:14
  • @Amongalen Then its a bug!. – Vinay Hegde Jul 16 '20 at 11:14
  • @Lino-Votedon'tsayThanks I tried just now to run it on www.jdoodle.com/online-java-compiler/ - it doesn't work for JDK 1.8.0_66 but does for newer versions. – Amongalen Jul 16 '20 at 11:15
  • @Amongalen interesting, does jdoodle use the openjdk or the oracle/sun-jdk? – Lino Jul 16 '20 at 11:16
  • 2
    It has been fixed in Java9, the bugreport can be found [here](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8031085), not sure, but maybe they made a backport of the fixes for some java8 versions – Lino Jul 16 '20 at 11:18
  • Yes it is looks like a bug in 1.8. – talex Jul 16 '20 at 11:34
  • 2
    @Lino following your remark, I found this possible workaround: DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter() – Paul Jul 16 '20 at 11:34

0 Answers0