Stylianos Gakis
03/24/2025, 4:05 PM2025-03-24T08:00:00.000001Z
Which indicates that it's in UTC time, hence the Z
afaik
But I may get an instant which is not Z
there, but some other timezone too
Is there a way for me to turn it into a LocalDateTime, but using whatever time-zone the string itself already came in?
So I want to do
Instant
.parse("2025-03-24T08:00:00.000001Z")
.toLocalDateTime(???)
But not sure what I should do for timezone.
The use-case is that my server is giving me a timestamp, and I want to show the date only as-is, so whatever the server had decided already. So if you are in different parts of the world you do not get different results (if I used TimeZone.currentSystemDefault()
) but I also don't want to just default to UTC and potentially change the date that I show if the timestamp was not in UTC.Dmitry Khalanskiy [JB]
03/24/2025, 4:14 PM2025-03-24T08:00:00.000001+03:00
, you want to obtain LocalDateTime(2025, 3, 24, 8, 0, 0, 1000)
?curioustechizen
03/24/2025, 4:22 PMStylianos Gakis
03/24/2025, 4:27 PMIf I understand the question correctly, you don't want to parse an Instant, you want to parse a LocalDateTime instead. But that expects the trailing Z to be absent.Yeah I think this is what I am looking for too, because parsing LocalDateTime fails for me there, because it does not like the
Z
(or whatever else) to be there
Yes, optimally. And I could even do without knowing about the millisecond accuracy, but I suspect that's not the biggest of deals there.?LocalDateTime(2025, 3, 24, 8, 0, 0, 1000)
eygraber
03/24/2025, 7:35 PMkevin.cianfarini
03/24/2025, 8:33 PMkevin.cianfarini
03/24/2025, 8:48 PMLocalDateTime
chunk as it’s represented in the ISO string (eg. 2025-03-24T08:00:00.000001
), then you could do this:
val localDateTime = Instant.parse("2025-03-24T08:00:00.000001Z).toLocalDateTime(TimeZone.UTC)
I think this will work for your use case because Instant
will always be normalized to UTC time. The conversion between UTC time and local time with the UTC timezone is a 1:1 mapping.eygraber
03/24/2025, 8:50 PMLocalDateTime
crashes if there is a Z
present and ZonedDateTime
crashes if there isn't one.kevin.cianfarini
03/24/2025, 8:51 PMZ
, for example:
val localDateTime = Instant.parse("2025-03-24T08:00:00.000001+03:00).toLocalDateTime(TimeZone.UTC)
Would spit out 2025-03-24T05:00:00.000001
local time.kevin.cianfarini
03/24/2025, 8:52 PMThe problem isBased on some of the above comments I’m not sure this is my understanding of the issue. Instead, @Stylianos Gakis wants to grab the local date time representation of an ISO-8601 timezone qualified stringcrashes if there is aLocalDateTime
present andZ
crashes if there isn’t one.ZonedDateTime
kevin.cianfarini
03/24/2025, 8:52 PMeygraber
03/24/2025, 8:53 PMStylianos Gakis
03/24/2025, 10:33 PMtoLocalDateTime(TimeZone.UTC)
wouldn't work because then I may end up changing the date if the incoming timestamp is not already in UTC.
It may for example be in time zone +5 and have time of 02:00, so if I do this conversion I will go back one day 😅
I basically want the backend to give me whatever date it wants to give me, and use that as what I show on the screen, regardless of where the user may be in the world.kevin.cianfarini
03/24/2025, 10:36 PMStylianos Gakis
03/24/2025, 10:42 PMcurioustechizen
03/25/2025, 7:29 AMDmitry Khalanskiy [JB]
03/25/2025, 7:35 AM