0

I have a data out of 50k reading line by line, one got a,

s = "2018-11-05T06:14:10.6-05:00".

Which will get error on,

public static DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
            .ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX");
            
if(s!=null && isValidDate(s)) {
   OffsetDateTime oDAtOffset = OffsetDateTime.parse(s, DATE_TIME_FORMATTER); //error!
}

If s = "2018-11-05T06:14:10.006-05:00" then it won't get error.

How do I convert from "2018-11-05T06:14:10.6-05:00" to "2018-11-05T06:14:10.006-05:00"?

Turing85
  • 18,217
  • 7
  • 33
  • 58
uniwinux
  • 19
  • 5
  • Check out https://stackoverflow.com/questions/36188428/java-8-date-equivalent-to-jodas-datetimeformatterbuilder-with-multiple-parser-f – Edward Peters Nov 22 '22 at 23:03
  • I'm not sure you can assume that 14:10.6 is 14:10:006. Since that field is milliseconds, .6 is more likely 600 ms. But it's ambiguous. If the data are sequential in time, then you should look at the lines before and after this one to see if there's a clue. – pfurbacher Nov 23 '22 at 01:41

2 Answers2

1

You can use the ISO 8601 DateTimeFormatter, which handles both variants:

var t = java.time.ZonedDateTime.parse("2018-11-05T06:14:10.6-05:00");
System.out.println(t);

Output:

2018-11-05T06:14:10.600-05:00

As already said: .6 is not equal to .006

Christoph Dahlen
  • 826
  • 3
  • 15
0

I just do it like this,

s = s.substring(21,22).equals("-") ? s.substring(0,21)+"00"+s.substring(21,27) : s;
uniwinux
  • 19
  • 5
  • This turns .6 into .006, which is different. – Christoph Dahlen Nov 25 '22 at 01:41
  • Did u test it? I'm getting this "2018-11-05T06:14:10.600-05:00". The problem if I used your library, my code will add 600+ bytes. It's like asking only for a banana but you got a gorrila holding a banana in a jungle. – uniwinux Nov 29 '22 at 00:05