Hi guys, I have a question which is driving me up ...
# announcements
v
Hi guys, I have a question which is driving me up the walls, how can I get yesterday EOD in a given time zone regardless of where my code is running (ie. JVM time). For example If I have a ZonedDateTime
May 1 2020 5:35:00 PM PST
I want to programatically get
April 30 2020 11:59:59 PM PST
I tried
Copy code
val currentTimeInTimeZone = ZonedDateTime.now(ZoneId.of(someTimeZone))

val yesterday = ZonedDateTime.of(
            LocalDate.now().minusDays(1).atTime(LocalTime.MAX).truncatedTo(ChronoUnit.SECONDS),
            ZoneId.of(someTimeZone)
        )
But it seems to work on my machine and when I push the code to production it does not return the correct value, any ideas / help would be appreciated
p
I’ve done it like this and it has seemed to work just fine.
Copy code
val zdt1 = ZonedDateTime.now()
    val zdt2 = zdt1.minusDays(1)
            .with(ChronoField.HOUR_OF_DAY, 23)
            .with(ChronoField.MINUTE_OF_HOUR, 59)
            .with(ChronoField.SECOND_OF_MINUTE, 59)
    println(zdt1)
    println(zdt2)
k
does not LocalDate.now() use system's zoneid?
p
Yeah, it does.
t
You want to start with the time in UTC I think, try this:
Copy code
val now = Instant.now()
val then = now.minus(1, ChronoUnit.DAYS).atZone(ZoneId.of("America/Los_Angeles")).truncatedTo(ChronoUnit.DAYS)
v
thanks guys, really appreciate it