yyyy-MM-dd'T'HH:mm:ss'Z The default formatters or ...
# kotlinx-datetime
j
yyyy-MM-dd'T'HHmmss'Z The default formatters or adapters for kotlinx serialization doesnt understand this UTC format used. I often fall back to Instant but ideally I want LocalDateTime. How do I do this the right way if any? I dont like using Instant and want avoid do custom parser. Want standard iso parsers from kotlinx only ideally.
d
That isn’t a local date time though, that is an instant, represented in UTC timezone. LocalDateTime doesn’t have a timezone, so it isn’t represented that way.
c
What are you trying to do exactly?
j
Parse regular utc datetime strings from server with built in classes and avoid expose Instant to formatter layer. Ideally want ICU standard after serialize it and then format. And Yes we want kept aritmetics, invariant and locale formatters. I cant combine DateTimeFormatter with kotlinx datetime if not parse properly into agnostic format supported.
c
Parse regular utc datetime strings
This is the definition of
Instant
avoid expose Instant to formatter layer
Why? Your data is an
Instant
j
Ok so Instant is always UTC format? And if doing so, whats recommended way of formatting per locale with region and country with ICU in jvm world?
I was under impression Instant was some kind of raw format similar to Javas Instant.
c
Instant
isn't a format,
Instant
is a representation of "an actual point in time". All other types represent something else. If you want to format per locale, you'd first convert your
Instant
to a local time by combining it with the timezone ID (
yourInstant.toLocalDateTime(tz)
) and then format that
j
Yes this whats confuse me. Instant parse utc string but kind a discard utc information. And when convert to local datetime need to add utc again. Why isnt serualuzer for LocalDateTime support utc strings but Instant does,?
The next issue is that I need convert kotlinx datetime back to java or three ten classes so I can use ICU format with datetime formatter in Java. Will kotlinx datetime add this in future? Looks like three ten extra do this.
c
> Why isnt serualuzer for LocalDateTime support utc strings but Instant does,? Because they are different things.
LocalDateTime
is "the time a clock shows".
Instant
is "a physical instant in the universal timeline".
Instant
is traditionally written and read in UTC, but that's completely arbitrary. If your server is sending you "something that happened at a given time", then it's an
Instant
, that's all there is to it.
j
So there will be no equivalent to ZonedDateTime or such? Ideally want one format that has built in serializer but also able format properly with time zones from server included.
c
What would you want to do with a
ZonedDateTime
? Everything you said here you can do with
Instant
without problems?
j
Except that Instant discarding time zone so I need to add it back before format string. I want formats like d MM, YYYY without adding custom formatter and same being y.MM.YYYY depending on Locale. And applied same for durations and time ofc.
I cant do like DateTimeFormatter.ofLocalized(style.Medium). format(Instant) or can I?
c
Your server is sending you an UTC timestamp. You want to print a localized date. At some point, you need to give the user's timezone to the system
j
Yes but want to use servers timezone not device timezone.
Ideally framework support all timezone strings and parse it automatic to correct class type whatever that may be and from that format same framework able to print/format with correct Locale. I dont want to go back to three ten, icu4j and/or JodaTime 😁
c
How would you know what the server timezone is?
j
It sends UTC time?
c
Sure, because UTC time is the default encoding for sharing data. That doesn't mean your server is in one of these cities
j
Yes I know :) Seems working now by using first Instant and then convert to LocaleDateTime and convert to Java LocalDateTime and then using DateTimeFormatter.ofLocalizedDateTime(medium). I was expected kotlinx datetime supported this but seems not. Back to java formatting again ...