I have `Instant` 's that I'm trying to convert to ...
# kotlinx-datetime
c
I have
Instant
's that I'm trying to convert to strings with the format of
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
My code seems to work in most cases BUT according to crashlytics... it's sometimes giving me dates that don't exist. Like weirdly large year values or just dates that don't exist like April 31st, even though april only has 30 days. I think it maybe is coming from the fact that I use SimpleDateFormat. Does that sound like something that could be the fault of SDF?
c
SDF is infamously not thread-safe. If you're not synchronizing access to the SDF object or using a new instance every time you're formatting a date, you're likely seeing the result of race conditions https://www.baeldung.com/java-simple-date-format#2-thread-safety
c
I saw that SDF isn't thread safe and maybe I need to brush up on threading and stuff. but im not sure how that could be the cause here. Each time I do the conversion I create a new SDF instance.
would love to be taught how that could still go wrong, because i dont see anything obvoisly wrong with going this route.
a
Did you try `kotlinx-datetime`'s new formatting API? https://pl.kotl.in/TnehLUMdJ (or, to use your string verbatim, https://pl.kotl.in/F7rkqYDPV) It should be thread-safe.
👍 1
m
Should there be a
char('.')
before the
secondFraction()
?
a
Yes, thanks! Edited my message with the fix.