Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherit the specified TimeZone when converting between Instant and LocalDateTime using toInstant and toLocalDateTime? #409

Open
ArcherEmiya05 opened this issue Jul 15, 2024 · 0 comments

Comments

@ArcherEmiya05
Copy link

ArcherEmiya05 commented Jul 15, 2024

I am trying to convert the following Java 8 Desugar DateTime.

        val currentDate = LocalDateTime.now().atOffset(ZoneOffset.UTC)

        val currentDateInMilli = currentDate.toInstant().toEpochMilli()
        val oldestDateInMilli = currentDate.minusYears(currentDate.year - 2009L).toInstant().toEpochMilli()
        val previousMonthInMilli = currentDate.minusMonths(1).toInstant().toEpochMilli()

I ended up with this

        val now = Clock.System.now()

        val currentDate = now.toLocalDateTime(TimeZone.UTC)

        val currentDateInMilli = currentDate.toInstant(TimeZone.UTC).toEpochMilliseconds()
        val oldestDateInMilli = now.minus(currentDate.year - 2009L, DateTimeUnit.YEAR, TimeZone.UTC).toEpochMilliseconds()
        val previousMonthInMilli = now.minus(1, DateTimeUnit.MONTH, TimeZone.UTC).toEpochMilliseconds()

What I noticed is the repeated setting of TimeZone, would it be good if there will be a default value on it by reusing the TimeZone used on its origin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant