如何获取特定时区的当前时间

huangapple go评论75阅读模式
英文:

How to get current time for timezone

问题

我有针对特定操作存储的UTC时间偏移,格式如下:(此处为UTC+2)

double utc = 2.0;

如何在Java中利用这个双精度时间偏移获取该时区的当前时间?

英文:

I have UTC time offsets for specific operations stored like this: (this one is for UTC+2)

double utc = 2.0;

How can I get current time for that timezone using this double time offset in Java?

答案1

得分: 5

你可以结合以下方法:

<!-- -->

OffsetDateTime odt = Instant.now().atOffset(ZoneOffset.ofTotalSeconds((int) (2.0*3600)));

代码演示

英文:

You may combine

<!-- -->

OffsetDateTime odt = Instant.now().atOffset(ZoneOffset.ofTotalSeconds((int) (2.0*3600)));

Code Demo

答案2

得分: 2

首先,您需要将 double 值转换为 ZoneOffset

double utc = 2.0;

ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) (utc * 3600));

然后,您可以获取当前时间:

OffsetDateTime now = Instant.now().atOffset(offset);

示例输出

2020-09-22T21:16:42.816236600+02:00
英文:

First you need to convert the double value to a ZoneOffset:

double utc = 2.0;

ZoneOffset offset = ZoneOffset.ofTotalSeconds((int) (utc * 3600));

You can then get the current time:

OffsetDateTime now = Instant.now().atOffset(offset);

Sample Output

2020-09-22T21:16:42.816236600+02:00

答案3

得分: 1

另外两个选择:

使用:

double utc = 2.0;

选择 1:

ZonedDateTime nowAlt1 = Instant.now().atZone(ZoneOffset.ofTotalSeconds((int) utc * 3600));

选择 2:

ZonedDateTime nowAlt2 = ZonedDateTime.ofInstant(Instant.now(), ZoneOffset.ofTotalSeconds((int) utc * 3600));

选择 1 和选择 2 的上下文:

public static void main(String[] args) {
    double utc = 2.0;
    ZonedDateTime nowAlt1 = Instant.now().atZone(ZoneOffset.ofTotalSeconds((int) utc * 3600));
    ZonedDateTime nowAlt2 = ZonedDateTime.ofInstant(Instant.now(), ZoneOffset.ofTotalSeconds((int) utc * 3600));

    System.out.println("nowAlt1: " + nowAlt1);
    System.out.println("nowAlt2: " + nowAlt2);
}

输出:

nowAlt1: 2020-09-22T23:15:00.254912800+02:00
nowAlt2: 2020-09-22T23:15:00.253915600+02:00

在这里阅读更多关于 java.time 的信息,将为您提供何时使用不同类型的 java.time 的想法:
https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime

英文:

Two other alternatives:

Using:

double utc = 2.0;

Alternative 1:

ZonedDateTime nowAlt1 = Instant.now().atZone(ZoneOffset.ofTotalSeconds((int) utc * 3600));

Alternative 2:

 ZonedDateTime nowAlt2 = ZonedDateTime.ofInstant(Instant.now(), ZoneOffset.ofTotalSeconds((int) utc * 3600));

Alternative 1 and 2 in context:

public static void main(String[] args) {
    double utc = 2.0;
    ZonedDateTime nowAlt1 = Instant.now().atZone(ZoneOffset.ofTotalSeconds((int) utc * 3600));
    ZonedDateTime nowAlt2 = ZonedDateTime.ofInstant(Instant.now(), ZoneOffset.ofTotalSeconds((int) utc * 3600));

    System.out.println(&quot;nowAlt1: &quot; + nowAlt1);
    System.out.println(&quot;nowAlt2: &quot; + nowAlt2);
}

Output:

nowAlt1: 2020-09-22T23:15:00.254912800+02:00
nowAlt2: 2020-09-22T23:15:00.253915600+02:00

Read more about java.time here, will give you an idea when to use the different types of java.time:
https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime

huangapple
  • 本文由 发表于 2020年9月23日 02:54:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64016071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定