How to set required timezone in LocalTime
from joda? Required timezone - Moscow.
Default constructor new LocalTime()
shows other time for me: it shows time, which is one hour ahead then in my timezone.
A LocalTime
doesn't have a time zone. That's the whole point of it.
If you want the LocalTime
for a particular instant in a particular time zone, you can use DateTime
and then toLocalTime
:
DateTime now = new DateTime(zone); // Urgh: see below
LocalTime localNow = now.toLocalTime();
Note that I would strongly advise having a dependency representing a clock to provide "the current instant" - you can then use a "system" implementation returning the current time via System.currentTimeMillis
or the like, but you can also use a "testing" implementation returning a predefined time, allowing you to write unit tests for any moment in time that you like.
With such a dependency, you'd probably have something like:
DateTime now = new DateTime(clock.getCurrentInstant(), zone);
LocalTime localNow = now.toLocalTime();
See more on this question at Stackoverflow