OffsetDateTime转换为LocalDate在不同计算机上产生不同的结果。

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

OffsetDateTime to LocalDate get different result on different computer

问题

我有一个 OffsetDateTime,我想要转换为 LocalDate。

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

public class HelloWorld{

     public static void main(String []args){

         OffsetDateTime billDate = OffsetDateTime.parse("2019-06-12T22:00:00-04:00");

        System.out.println(billDate);

        System.out.println(billDate.toLocalDate());
     }
}

在某些计算机上,我得到
2019-06-12

而在另一些计算机上,我得到
2019-06-13

Java 根据计算机的时区进行转换吗?

英文:

I have a offsetDateTime that I search to convert to LocalDate.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;

public class HelloWorld{

     public static void main(String []args){
         
         OffsetDateTime billDate = OffsetDateTime.parse("2019-06-12T22:00:00-04:00");             
         
        System.out.println(billDate);
        
        System.out.println(billDate.toLocalDate());
     }
}

On some computer I get
2019-06-12

and on some other, I get
2019-06-13

Java do an conversion depending of the time zone of the computer?

答案1

得分: 1

答案:

> Java根据计算机的时区进行转换吗?

不会。

OffsetDateTime.toLocalDate的文档已经足够清楚:

> 获取此日期时间的LocalDate部分。
>
> 这将返回一个具有与此日期时间相同的年份、月份和日期的LocalDate

因此,无论JVM或计算机的时区设置如何,您始终会从您问题中的代码中获得2019-06-12,而不会是2019-06-13

我还在Europe/Copenhagen时区(对应时间为2019-06-13T04:00:00+02:00)上在Java 8、9和11上运行了您的代码。每次都得到2019-06-12

文档链接OffsetDateTime.toLocalDate()

英文:

To answer the question as asked:

> Java do an conversion depending of the time zone of the computer?

No.

The documentation of OffsetDateTime.toLocalDate is clear enough:

> Gets the LocalDate part of this date-time.
>
> This returns a LocalDate with the same year, month and day as this
> date-time.

So you will always get 2019-06-12 from the code in your question, never 2019-06-13, no matter the time zone setting of the JVM or of the computer.

I have also run your code on Java 8, 9 and 11 in Europe/Copenhagen time zone (where the corresponding time is 2019-06-13T04:00:00+02:00). I got 2019-06-12 each time.

Documentation link: OffsetDateTime.toLocalDate().

答案2

得分: 0

你可以通过调用 billDate.atZoneSimilarLocal(ZoneId.systemDefault()) 将一个时区中的时间转换为你所在时区的相同本地时间。

这就是处理这种问题的一种方法。但是,就像评论中的Chris所说,我无法复制你的确切问题。toLocalDate() 对我来说似乎可以完成我所假设你想要的功能,并将你指定的偏移时区的OffsetDateTime的日期部分提取出来(而不是相对于你自己的时区)。

英文:

You can convert the time in one timezone to the same local time in your timezone by calling billDate.atZoneSimilarLocal(ZoneId.systemDefault())

So that would be a way of approaching this sort of problem. But, like Chris in the comment, I can't reproduce your exact problem. toLocalDate() seems to me to do what I assume you want, and take the date part of your OffsetDateTime local to the zone offset you specified in it (not local to your own zone).

huangapple
  • 本文由 发表于 2020年7月23日 03:22:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63041694.html
匿名

发表评论

匿名网友

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

确定