0

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);
    
fernando1979
  • 1,727
  • 2
  • 20
  • 26
  • 2
    1. Yes 2. Yes 3. Not exactly, an old-fashioned `Date` object just stores a number of milliseconds since 01-01-1970, 00:00:00 UTC. It doesn't know anything about timezones. That the reference point is UTC does not mean it only stores UTC date and times. 4. What do you mean? That code takes a `LocalDate` and combines it with timezone information to create a `ZonedDateTime`, which *does* contain timezone information. – Jesper Sep 28 '22 at 18:12
  • 1
    To reduce confusion I recommend you don’t use the no-arg `LocalDate.now` method but rather `LocalDate.now(ZoneId.of("Europe/Madrid"))` or `LocalDate.now(ZoneOffset.UTC))` (in your example these two will give different results). – Ole V.V. Sep 28 '22 at 18:22

1 Answers1

2

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
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 1
    About question 2, what I meant is that when you create a LocalDate, it is always considered the timezone of the system or the zoneId you pass as a parameter to create the date, but once you already have the date created, if you print it is not considered the timezone, it was at the time of creating it, not when printing it. That was my question – fernando1979 Sep 28 '22 at 21:36
  • @fernando1979 If I have understood your comment correctly, [this](https://stackoverflow.com/a/67040099/10819573) can be helpful to you. – Arvind Kumar Avinash Sep 28 '22 at 22:13
  • 3
    I think the important distinction here is between a `LocalDate` *object* and the static no-arg `now` method of the `LocalDate` *class*. The object *never* considers the system time zone. The no-arg method *always* does. – Ole V.V. Sep 29 '22 at 01:19
  • 3
    @fernando1979 If I understand you correctly, you are correct: When you create the `LocalDate` using one of the `now` methods, time zone is considered (and system time zone of none is given in the call). After that the `LocalDate` object is never altered again, so when you print it, all that is considered is the state it got in creation. – Ole V.V. Sep 29 '22 at 06:09