简体   繁体   English

在不使用系统时间的情况下以Java获取UTC

[英]Get UTC in java without using system time

I want to change the Timezone from local timezone to use UTC in my application. 我想将时区从本地时区更改为在应用程序中使用UTC。 In place of local time I need to get UTC time, which is working perfectly with Joda datetime. 我需要获取UTC时间来代替本地时间,该时间与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. 但是问题是当我在Windows上更改系统时间时(例如,减去时钟1小时),UTC时间返回了一个小时的返回值。 So I think UTC time is calculated based on system time. 因此,我认为UTC时间是根据系统时间计算的。 Eg: system time is 3:00 PDT, UTC shows up 10:00. 例如:系统时间是太平洋标准时间3:00,UTC则显示10:00。 When I change system time to 4:00 PDT, UTC is 11:00. 当我将系统时间更改为PDT 4:00时,UTC为11:00。 Though system time has changed UTC should still show current UTC time and not based on sys time. 尽管系统时间已更改,但UTC仍应显示当前UTC时间,而不是基于系统时间。 Looks like offset is calculated based on systemtime to get the UTC. 看起来偏移是根据系统时间计算得出的,以获取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 我在Windows机器上将系统时间更改为4:00,现在输出为

    mypdt--> 2015-07-21T04:00:14.778-07:00
    myutc--> 2015-07-21T11:00:14.828Z

But actual UTC is still 10:00. 但是实际的UTC仍然是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. 如何获得不使用系统时间的UTC,因为在DST期间将重置系统时钟,这就是我要解决的问题。

如果您希望获得与系统时间无关的UTC时间,则可能需要通过API从外部资源中获取它,例如http://www.timeapi.org/https://developers.google.com。 / maps / documentation / timezone / intro

Though system time has changed UTC should still show current UTC time and not based on sys time. 尽管系统时间已更改,但UTC仍应显示当前UTC时间,而不是基于系统时间。

Why should it? 为什么要这样 PDT is a timezone with a specific offset (-7) with respect to UTC. PDT是相对于UTC具有特定偏移(-7)的时区。 So if you change your local time from 3:00 PDT to 4:00 PDT, the UTC naturally changes with it. 因此,如果您将本地时间从3:00 PDT更改为4:00 PDT,那么UTC自然会随之更改。

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). 如果您的目标是在不更改时间的情况下更改本地时间,则必须修改时区(例如更改为PST)或使用LocalDateTime (不包含时区)。

... when I change the system time on windows, say minus 1 hour on the clock, UTC time is returning value an hour back. ...当我在Windows上更改系统时间时(例如,时钟减去1小时),UTC时间将返回一个小时的值。 So I think UTC time is calculated based on system time. 因此,我认为UTC时间是根据系统时间计算的。

You seem to misunderstand a key point. 您似乎误解了关键点。 The system time is tracked in UTC only. 仅在UTC中跟踪系统时间。 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. 当您在任务栏上看到时钟或通过应用程序或编程框架获取当前本地时间时,系统时间将从UTC 转换为本地时区。

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. 设置计算机时间时,您将使用本地时间进行设置,该本地时间会立即转换为UTC,然后再分配给内部系统时钟。

... because system clock will be reset during DST and that is the problem I'm trying to resolve. ...因为系统时钟将在DST期间重置,这就是我要解决的问题。

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. 您始终可以从系统获取UTC时间,而不必担心DST更改会更改UTC时间。

In other words, for the next DST transition the clock will tick forward as follows: 换句话说,对于下一个DST转换,时钟将向前滴答如下:

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. 请注意,UTC时间不会更改,只有本地时间会更改。 Also note that the local offset changed, which Windows writes to the ActiveTimeBias registry value at HKLM\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation 还要注意,本地偏移已更改,Windows将其写入ActiveTimeBias注册表值,位于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. 当然,该系统时间还可以校正时钟漂移时改性 ,由用户,由OS,或经由NTP同步。 But you don't have to worry about DST modifications. 但是您不必担心DST修改。

You may also be wondering about the BIOS. 您可能还想知道BIOS。 Windows does write to the BIOS using local time, but on startup it reads that value and converts to UTC and tracks UTC. Windows确实使用本地时间写入BIOS,但是在启动时它将读取该值并转换为UTC并跟踪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. 当设置本地时间或启动DST时,它还可以管理写回BIOS的操作。这对大多数程序都是透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM