在特定的TimeZone中获取当前的Instant [英] Getting the current Instant in a specific TimeZone

查看:37
本文介绍了在特定的TimeZone中获取当前的Instant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在特定的timeZone上获取当前实例,但是它无法按预期工作.有关如何执行此操作的任何想法?

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 ?

这就是我所做的:

Instant.now(Clock.system(ZoneId.of("America/Los_Angeles"))).truncatedTo(ChronoUnit.SECONDS)

但是,返回的即时时间始终是UTC.我多次更改了ZoneID,但始终是错误的.请告知.

However the instant time returned is always UTC. I changed many time the ZoneID and it is always wrong. Please advise.

我正在与一个使用timeStamp生成日志的应用程序进行交互,因此我需要对这些事件进行操作.如果我立即使用特定的TimeStamp启动程序,则应该立即从该TimeStamp中读取事件.当我的笔记本电脑与生成这些事件的应用程序位于同一TimeZone时,当我获取Instant.Now()时,我似乎处于UTC状态.当应用程序根据我们所在的时区生成timeStamp时.我希望程序的时钟与服务器中的时钟相同.

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.

应用程序生成格式为2016-08-04T18:17:51Z的时间戳

The application generate timestamp of the form 2016-08-04T18:17:51Z

推荐答案

Instant不支持时区.如果需要时区感知时间戳,则应使用ZonedDateTime.

Instant are not Timezone aware. If you need a time zone aware timestamp, you should use ZonedDateTime.

签出 ZonedDateTime.now()

如果要解析ZonedDateTime并覆盖时区,则可以使用

If you want to parse a ZonedDateTime and override the timezone, you can use ZonedDateTime.withZoneSameLocal or ZonedDateTime.withZoneSameInstant depending on your need.

这篇关于在特定的TimeZone中获取当前的Instant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆