Reputation: 3
I want to change the Timezone from local timezone to use UTC in my application. In place of local time I need to get UTC time, which is working perfectly with Joda datetime. But the problem is when I change the system time on windows, say minus 1 hour on the clock, UTC time is returning value an hour back. So I think UTC time is calculated based on system time. Eg: system time is 3:00 PDT, UTC shows up 10:00. When I change system time to 4:00 PDT, UTC is 11:00. Though system time has changed UTC should still show current UTC time and not based on sys time. Looks like offset is calculated based on systemtime to get the UTC.
DateTime mypdt = new DateTime();
DateTime myutc = new DateTime(DateTimeZone.UTC);
System.out.println("mypdt--> "+mypdt);
System.out.println("myutc--> "+myutc);
Output is
mypdt--> 2015-07-21T03:00:14.778-07:00
myutc--> 2015-07-21T10:00:14.828Z
I changed the system time to 4:00 on my windows machine and now the output is
mypdt--> 2015-07-21T04:00:14.778-07:00
myutc--> 2015-07-21T11:00:14.828Z
But actual UTC is still 10:00. How can I get UTC that will not use system time because system clock will be reset during DST and that is the problem I'm trying to resolve.
Upvotes: 0
Views: 1381
Reputation: 241450
... when I change the system time on windows, say minus 1 hour on the clock, UTC time is returning value an hour back. So I think UTC time is calculated based on system time.
You seem to misunderstand a key point. The system time is tracked in UTC only. When you see the clock on the taskbar or get the current local time via an application or programming framework, the system time is converted from UTC to the local time zone.
When you set the computer's time, you're setting using the local time, which is immediately converted to UTC before being assigned to the internal system clock.
... because system clock will be reset during DST and that is the problem I'm trying to resolve.
No, it won't. The system time ticks forward as normal. You can always get the UTC time from the system without worrying about it being altered for DST changes.
In other words, for the next DST transition the clock will tick forward as follows:
UTC Pacific
2015-11-01T08:59:58Z 2015-11-01T01:59:58-07:00
2015-11-01T08:59:59Z 2015-11-01T01:59:59-07:00
2015-11-01T09:00:00Z 2015-11-01T01:00:00-08:00
2015-11-01T09:00:01Z 2015-11-01T01:00:01-08:00
Note that the UTC time did not change, only the local time did. Also note that the local offset changed, which Windows writes to the ActiveTimeBias
registry value at HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
Of course, the system time can still be modified, either by the user, by the OS when correcting for clock drift, or via NTP synchronization. But you don't have to worry about DST modifications.
You may also be wondering about the BIOS. Windows does write to the BIOS using local time, but on startup it reads that value and converts to UTC and tracks UTC. It also manages writing back to the BIOS when setting the local time, or when DST kicks in. This is transparent to most programs.
Upvotes: 0
Reputation: 4361
If you want to get UTC time independent of your system time, then probably you need to retrieve it from an external source through API, maybe this http://www.timeapi.org/ or this https://developers.google.com/maps/documentation/timezone/intro
Upvotes: 1
Reputation: 12932
Though system time has changed UTC should still show current UTC time and not based on sys time.
Why should it? PDT is a timezone with a specific offset (-7) with respect to UTC. So if you change your local time from 3:00 PDT to 4:00 PDT, the UTC naturally changes with it.
If your goal is to change the local time without changing the instant you will have to modify the time zone (for example change to PST) or use LocalDateTime
(which does not contain a time zone).
Upvotes: 0