Getting the current Instant in a specific TimeZone
Instant
are not Timezone aware. If you need a time zone aware timestamp, you should use ZonedDateTime
.
Check out ZonedDateTime.now(timezone)
which will use the provided timezone. To use the machine's default timezone, use ZonedDateTime.now()
If you want to parse a ZonedDateTime and override the timezone, you can use ZonedDateTime.withZoneSameLocal
or ZonedDateTime.withZoneSameInstant
depending on your need.
MaatDeamon
Updated on July 18, 2022Comments
-
MaatDeamon almost 2 years
I have tried to get the current Instance on a specific timeZone but it does not work as expected. Any idea on how to do this ?
Here is what i did:
Instant.now(Clock.system(ZoneId.of("America/Los_Angeles"))).truncatedTo(ChronoUnit.SECONDS)
However the instant time returned is always UTC. I changed many time the ZoneID and it is always wrong. Please advise.
EDIT:
I'm interacting with an application that generate log with timeStamp and i need to operate over those event. For instant if I start my program with a specific TimeStamp it should start reading event from that TimeStamp. While my laptop is in the same TimeZone as the application that generate those event, when i get Instant.Now() i seem to be in UTC. While the application generate timeStamp according to the TimeZone in which we are. I want the clock of my program to be the same as the one in the Server.
The application generate timestamp of the form 2016-08-04T18:17:51Z
-
MaatDeamon almost 8 yearsthank you. I got confused with the timestamp of the system, they are actually UTC. I made a mistake but, thanks for the input, it helps clarifying everything