I need to figure out a way to get the user's local time reliably. After researching, one potential solution could be:
Day of the week in user's location:
int dayOfWeek = ZonedDateTime.now(Clock.systemDefaultZone()).getDayOfWeek().ordinal();
Actual time in the user's location:
LocalDateTime.now(Clock.systemDefaultZone()));
Is this all correct? Am I correct to be using Clock.systemDefaultZone()
? If so, I wonder how Clock.systemDefaultZone()
is able to magically figure out the correct time zone?
Note that in your first example, you don't need a
ZonedDateTime
, aLocalDate
would do since you only care about the day in the week, not the time in that day. Also note thatLocalDate.now()
uses the system default clock and you don't need to explicitly specifly it.Regarding that default clock,
Clock.systemDefaultZone()
is based on the system default time zone, which is retrieved by queryingTimeZone.getDefault()
: