To understand those results, we must first see how the Clock
is intended to work. Taking a look at the javadoc, we can see the following description for the methods:
public abstract Instant instant()
Gets the current instant of the clock.
This returns an instant representing the current instant as defined by the clock.
public abstract ZoneId getZone()
Gets the time-zone being used to create dates and times.
A clock will typically obtain the current instant and then convert that to a date or time using a time-zone. This method returns the time-zone used.
So the instant()
method will get the current instant as a java.time.Instant
, which is a class that always works in UTC. And the key point here is: "as defined by the clock".
The Clock
class allows you to create lots of different clock definitions - such as a fixed clock that always returns the same thing - and the most common is the one returned by systemDefaultZone()
, which uses the system's current date/time.
As the instant()
method returns a java.time.Instant
and this class works only in UTC, the result will always be UTC.
The getZone()
method will return the timezone used to create dates and times, and this is done by combining the Instant
(returned by instant()
) with the ZoneId
returned by getZone()
.
You can create a clock with any timezone you want, but systemDefaultZone()
just uses the JVM default timezone, which is - in your case - Europe/Moscow.
When you call LocalTime.now()
, it internally uses the clock returned by systemDefaultZone()
.
Then, it uses the results from instant()
and getZone()
, and combine both to get the LocalTime
.
Usage
According to javadoc:
Use of a Clock is optional. All key date-time classes also have a now() factory method that uses the system clock in the default time zone. The primary purpose of this abstraction is to allow alternate clocks to be plugged in as and when required. Applications use an object to obtain the current time rather than a static method. This can simplify testing.
So I wouldn't use the clock directly. Instead, I'd use the now
methods from each class, depending on what I need.
- If I want the current moment in UTC:
Instant.now()
- only the current date:
LocalDate.now()
and so on...
The now()
method without parameters can be very handy and convenient, but it has some drawbacks. It always uses the JVM default timezone behind the scenes. The problem, though, is that the default timezone can be changed at anytime, either by JVM/system's config or even by any application running in the same VM, and you have no control over it.
To not depend on the default configurations, you can use the alternatives now(ZoneId)
(which uses an explicity timezone), or now(Clock)
, which makes your code more testable - see examples here.