Time.toMillis (boolean) does not include time zone

Consider the following code:

Time time1 = new Time("America/Los_Angeles"); // UTC -8 Time time2 = new Time("Pacific/Kiritimati"); // UTC +14 time1.setToNow(); time2.setToNow(); Log.d("timetest", "America/Los_Angeles: " + time1.toMillis(false)); Log.d("timetest", "Pacific/Kiritimati : " + time2.toMillis(false)); 

Here is the log that gets the output:

 08-03 07:27:50.687: D/timetest(28411): America/Los_Angeles: 1375540070000 08-03 07:27:50.687: D/timetest(28411): Pacific/Kiritimati : 1375540070000 

As you can see, the millisecond values ​​are identical, even if they are full 22 hours depending on time zones. This makes me think that Time.toMillis(boolean) based on UTC, not on the time zone of Time . I need a way to get milliseconds based on the time zone of Time . Any ideas?

+4
source share
2 answers

The number of milliseconds since the era of January 1, 1970 00:00:00 UTC is the same everywhere. It doesn’t matter if you are currently in Los Angeles or Kiritimati.


EDIT

The toMillis method returns the number of milliseconds since an era. The era of January 1, 1970 00:00:00 UTC.

Think of it this way. If person A was born in Los Angeles in the era, and person B was born in Kiritimati in the era, then the age of person A would be time1.toMillis() , and the current age of person B would be time2.toMillis() , right?

Although the UTC time zone was midnight when they were born, it was not midnight in their respective time clock. You can find out by their time zones: UTC-8 and UTC + 14.

Now, if time1.toMillis() != time2.toMillis() this will mean that one of the two people is older. This would mean that he lived more than a millisecond in his life. Of course, this is not so, because, as we said, they were born at the same time. Therefore, the conclusion that you see is correct , and I do not think that you should try to change it.

+6
source

Found this out with Time.gmtoff , which contains the UTC offset in seconds.

 Time time1 = new Time("America/Los_Angeles"); // UTC -8 Time time2 = new Time("Pacific/Kiritimati"); // UTC +14 time1.setToNow(); time2.setToNow(); Log.d("timetest", "America/Los_Angeles: " + (time1.toMillis(false) - (time1.gmtoff * 1000))); Log.d("timetest", "Pacific/Kiritimati : " + (time2.toMillis(false) - (time2.gmtoff * 1000))); 

Resulting Log:

 08-03 07:53:04.866: D/timetest(31414): America/Los_Angeles: 1375566784000 08-03 07:53:04.866: D/timetest(31414): Pacific/Kiritimati : 1375491184000 
0
source

Source: https://habr.com/ru/post/1495104/


All Articles