在特定的TimeZone中获取当前的Instant

编程入门 行业动态 更新时间:2024-10-24 13:21:32
本文介绍了在特定的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(timezone) ,它将使用提供的时区.要使用计算机的默认时区,请使用 ZonedDateTime.now()

如果要解析ZonedDateTime并覆盖时区,则可以使用 ZonedDateTime.withZoneSameLocal 或 ZonedDateTime.withZoneSameInstant ,具体取决于您的需求.

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

本文发布于:2023-11-09 23:01:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在特定   TimeZone   Instant

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!