fernando1979
fernando1979

Reputation: 1947

LocalDate in java stores UTC date or local date?

reading documentation about LocalDate in Java, the documentation says LocalDate.now() "Obtains the current date from the system clock in the default time-zone.". So if I am in Madrid-Spain and lets imagine right now is 28-09-2022 01:12(GMT+02:00)

  1. The date stored in LocalDate is 28-09-2022, not 27-09-2022 that would be UTC date. Right?

  2. When you print a LocalDate, it is not considered the timezone of the system to print it because LocalDate does not have any timezone information, just print the value stored, right?.

  3. So, there are differences with Date objects because Date objects store UTC date and times, right?

  4. The documentation says "This class does not store or represent a date or time-zone", so in order to do the next, how can it be done the second sentence if localDate has not timezone information?

        LocalDate localDate = LocalDate.now();
    
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("EST5EDT"));
    
        System.out.println(zonedDateTime);
    

Upvotes: 1

Views: 4299

Answers (2)

John
John

Reputation: 126

I don't think Question # 4 was answered well. I think it is obvious to Jasper and others but not so obvious to me and I think OP.

LocalDateTime.now() will use either system clock or provided timezone to determine what "now" is, but then removes the time zone reference. It is impossible to determine what original time zone was used to create the LocalDateTime object.

That had me wondering how does LocalDateTime.atZone(ZoneId.of("Europe/Madrid")) properly convert the LocalDateTime to Europe/Madrid timezone if there is no time zone reference point to convert from? For example, if LocalDateTime is 2022-09-28T21:03:56, how can it then convert that to Europe/Madrid when it does not understand what time zone the date/time it holds originated from?

The solution is obvious now. It doesn't convert and it does not try to figure out what timezone it originated from. It just appends the timezone specified to the date/time it has.

So if a LocalDateTime object has a value of 2022-09-28T21:03:56, then LocalDateTime.atZone(ZoneId.of("Europe/Madrid")) instantiates a ZonedDateTime object with a value of 2022-09-28T21:03:56+01:00. Notice there is no change other than adding the +01:00 offset in use by the people of that time zone at that moment.

If that particular time-of-day does not exist in that time zone at that moment, such as during a Daylight Saving Time (DST) cutover, the time-of-day in the new ZonedDateTime is adjusted logically.

After I understood those two points it was all really clear how it worked.

Hope this helps someone else that this was not that obvious for.

Upvotes: 0

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78945

enter image description here

Jesper has already answered all your questions. However, it seems he perceived your Q#2 in a way different from what you may be thinking. Your Q#2 is:

When you print a LocalDate, it is not considered the timezone of the system to print it because LocalDate does not have any timezone information, just print the value stored, right?.

The fact is, when you print a LocalDate, it always considers the timezone of the system e.g. if you print LocalDate.now() in Madrid, India and New York at the same time, you may get different results (depending on what time you do it). Probably, this is what Ole V.V. has tried to tell you in his comment.

Demo:

import java.time.LocalDate;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        System.out.println(LocalDate.now(ZoneId.of("Europe/Madrid")));
        System.out.println(LocalDate.now(ZoneId.of("Asia/Kolkata")));
        System.out.println(LocalDate.now(ZoneId.of("America/New_York")));
    }
}

Output:

2022-09-28
2022-09-29
2022-09-28

To understand this difference, I suggest you print LocalDateTime instead of just LocalDate e.g.

import java.time.LocalDateTime;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        System.out.println(LocalDateTime.now(ZoneId.of("Europe/Madrid")));
        System.out.println(LocalDateTime.now(ZoneId.of("Asia/Kolkata")));
        System.out.println(LocalDateTime.now(ZoneId.of("America/New_York")));
    }
}

Output:

2022-09-28T21:03:56.438167
2022-09-29T00:33:56.443577
2022-09-28T15:03:56.444049

Upvotes: 2

Related Questions