robert trudel
robert trudel

Reputation: 5779

OffsetDateTime to LocalDate get different result on different computer

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?

Upvotes: 0

Views: 3439

Answers (2)

Anonymous
Anonymous

Reputation: 86379

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().

Upvotes: 2

Andrew McGuinness
Andrew McGuinness

Reputation: 2147

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).

Upvotes: 0

Related Questions