Skip to content

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

Description

@ArcherEmiya05

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions