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.
Jon Skeets advise about an injectable clock is nice and good. Otherwise - and closer to your question - you should use the alternative constructor of LocalTime
accepting a DateTimeZone
-argument in order to not rely on your system timezone which might not be what you want:
LocalTime time = new LocalTime(DateTimeZone.forID("Europe/Moscow"));
How does it work?
First Joda-Time uses the standard system clock via System.currentTimeMillis()
and gets a global timestamp. In order to derive the local time part Joda-Time must know a timezone reference. That is what you define here. The zone is only used for the conversion, nothing else. After the construction of the new local time (which has no internal knowledge about timezones), Joda-Time no longer needs the zone reference as documented.
Update:
In case your system timezone is already "Europe/Moscow" (the constructor without args uses the system tz!) you should check your Joda-Time-version. Maybe you have outdated timezone data containing old dst rules. Downloading the newest version v2.8 would probably help against old data.