So, I started out with something like...
String text = "2021-03-10 16:37:02.4230000";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSSS");
LocalDateTime ldt1 = LocalDateTime.parse("2021-03-10 16:37:02.4230000", formatter);
DateTimeFormatter shortFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
System.out.println(shortFormatter.format(ldt1));
LocalDateTime ldt2 = LocalDateTime.parse("2021-03-10 16:39:51.0000000", formatter);
System.out.println(shortFormatter.format(ldt2));
Which prints out ...
2021-03-10 16:37:02.4
2021-03-10 16:39:51.0
Hmmm , not quite what we're looking for.
Lucky for us, there's the DateTimeFormatterBuilder class. So next, I tried something like...
DateTimeFormatter toFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm:ss")
.appendFraction(ChronoField.MILLI_OF_SECOND, 1, 9, true)
.toFormatter();
System.out.println(toFormatter.format(ldt1));
System.out.println(toFormatter.format(ldt2));
Which prints out ...
2021-03-10 16:37:02.423
2021-03-10 16:39:51.0
Success
Now, please note, I've not really used DateTimeFormatterBuilder before, so there might be some other, really super awesome cool way to achieve the same or better result, but hay, it's a nice start