Jesper has already given you short answers to your questions.
The no-arg method LocalDate.html#now obtains the current date from the system clock in the default (system) time-zone. Therefore, when 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).
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
Note: to understand this difference, check the output of the following program:
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")));
}
}
Result:
2022-09-28T21:03:56.438167
2022-09-29T00:33:56.443577
2022-09-28T15:03:56.444049
However, a LocalDate object does not store the time-zone information. Therefore, when you print an object of LocalDate (i.e. LocalDate#toString), the printed value will remain the same irrespective of the system time-zone.
A java.util.Date object too does not store the time-zone information. It stores the number of milliseconds from January 1, 1970, 00:00:00 GMT. However, when you print an object of java.util.Date (i.e. Date#toString), it uses the default (system) time-zone to print the value i.e. if you print an object of java.util.Date on systems with different time-zones set to them, you will get different results.
Videos
Jesper has already given you short answers to your questions.
The no-arg method LocalDate.html#now obtains the current date from the system clock in the default (system) time-zone. Therefore, when 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).
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
Note: to understand this difference, check the output of the following program:
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")));
}
}
Result:
2022-09-28T21:03:56.438167
2022-09-29T00:33:56.443577
2022-09-28T15:03:56.444049
However, a LocalDate object does not store the time-zone information. Therefore, when you print an object of LocalDate (i.e. LocalDate#toString), the printed value will remain the same irrespective of the system time-zone.
A java.util.Date object too does not store the time-zone information. It stores the number of milliseconds from January 1, 1970, 00:00:00 GMT. However, when you print an object of java.util.Date (i.e. Date#toString), it uses the default (system) time-zone to print the value i.e. if you print an object of java.util.Date on systems with different time-zones set to them, you will get different results.
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.