英文:
Timezone methods not changing the current Instant time
问题
我想返回特定时区(TimeZone)的当前时间(Instant):我尝试了以下代码块:
Instant now2 = Instant.now();
ZonedDateTime zdt = ZonedDateTime.ofInstant(now2, ZoneId.of("Asia/Singapore"));
System.out.println("Singapore" + zdt.toInstant());
或者也可以尝试:
Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
或者:
Instant timeStamp = Instant.now();
ZonedDateTime timestampAtGMTPlus1 = timeStamp.atZone(ZoneId.of("GMT+02:00"));
Instant nowGMT = timestampAtGMTPlus1.toInstant();
System.out.println("In 'GMT+01:00' Time Zone:" + nowGMT);
但似乎都没有产生与当前时间(Instant.now()
)不同的时间。是否需要先进行时区设置?我的 Instant 类(Java 8)有什么问题?
更新:在这三种方法中,结果都是 2020-08-04T16:08:59.062Z
。
对于 Mathew:您的解决方案输出 23:08:59.063594500
,这对我来说没有提供足够的信息。
英文:
I want to return the current time (Instant) in a certain TimeZone
: I tried with the following chunks of code :
Instant now2 = Instant.now();
ZonedDateTime zdt = ZonedDateTime.ofInstant(now2,
ZoneId.of("Asia/Singapore"));
System.out.println("Singapore"+ zdt.toInstant());
with this :
Instant nowUtc = Instant.now();
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");
ZonedDateTime nowAsiaSingapore = ZonedDateTime.ofInstant(nowUtc, asiaSingapore);
or also :
Instant timeStamp= Instant.now();
ZonedDateTime timestampAtGMTPlus1= timeStamp.atZone(ZoneId.of("GMT+02:00"));
Instant nowGMT = timestampAtGMTPlus1.toInstant();
System.out.println("In 'GMT+01:00' Time Zone:"+ nowGMT);
But neither seems to be producing any different time from the current one (Instant.now()
)
Is there a prior config to set for the timezone to be modified ?
What's wrong with my Instant Class (Java 8) ?
UPDATE : in all three the result is 2020-08-04T16:08:59.062Z
For Mathew : your solution is outputting : 23:08:59.063594500
which is a no-information to me.
答案1
得分: 1
Instant总是UTC时间。你要找的类叫做LocalTime
:
Instant now = Instant.now();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.of("Asia/Singapore"));
System.out.println("The time in Singapore is: " + zonedDateTime.toLocalDateTime());
zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.of("Europe/London"));
System.out.println("The time in London is: " + zonedDateTime.toLocalDateTime());
输出结果:
新加坡的时间是:2020-08-04T23:21:00.238406800
伦敦的时间是:2020-08-04T16:21:00.238406800
英文:
Instant is always UTC. The class you are looking for is called LocalTime
:
Instant now = Instant.now();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.of("Asia/Singapore"));
System.out.println("The time in Singapore is: " + zonedDateTime.toLocalDateTime());
zonedDateTime = ZonedDateTime.ofInstant(now, ZoneId.of("Europe/London"));
System.out.println("The time in London is: " + zonedDateTime.toLocalDateTime());
Gives:
The time in Singapore is: 2020-08-04T23:21:00.238406800
The time in London is: 2020-08-04T16:21:00.238406800
答案2
得分: 0
你可以尝试这样做:
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");
Instant inst = LocalDateTime.now(asiaSingapore).toInstant(ZoneOffset.UTC);
System.out.println(inst);
英文:
You may try like this
ZoneId asiaSingapore = ZoneId.of("Asia/Singapore");
Instant inst = LocalDateTime.now(asiaSingapore).toInstant(ZoneOffset.UTC);
System.out.println(inst);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论