英文:
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
你可以结合以下方法:
ZoneOffset.ofTotalSeconds来从小时偏移获取偏移时区Instant.atOffset来从当前的Instant和ZoneOffset获取OffsetDatetime
<!-- -->
OffsetDateTime odt = Instant.now().atOffset(ZoneOffset.ofTotalSeconds((int) (2.0*3600)));
代码演示
英文:
You may combine
ZoneOffset.ofTotalSecondsto get the offset zone from the hour offsetInstant.atOffsetto get theOffsetDatetimefrom currentInstantand theZoneOffset
<!-- -->
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("nowAlt1: " + nowAlt1);
    System.out.println("nowAlt2: " + 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论