Is there any way by which I can get TimeZone value using time stamp

人走茶凉 提交于 2021-02-11 15:40:38

问题


This is the value which I have,

Sun Mar 29 2020 02:55:00 GMT+0530

and I want to get,for example

Asia/Calcutta

as ouput. Thanks in advance.


回答1:


Offset does not indicate zone

get TimeZone value using time stamp

No.

You cannot determine a time zone from an offset.

Many time zones can share the same offset-from-UTC (the number of hours-minutes-seconds ahead or behind the prime meridian).

See the list of time zone names in Wikipedia. Click on the column header to sort by offset. Notice how often several zones share the same offset.

Specific to your example, notice how we currently have two zones that coincidentally share an offset of five and a half hours ahead of UTC:

  • Asia/Kolkata (India)
  • Asia/Colombo (Sri Lanka)

So, without further input, there is no way to know if the author of your input string intended India time or Sri Lanka time.

By the way, the name Asia/Calcutta has been changed to Asia/Kolkata. If your system has no such name, then your tzdata is several years out of date. Always keep all the copies of tzdata up-to-date in OSes, database servers such as Postgres, and runtimes such as Java.

Another complication: politicians frequently change the offset used in their jurisdictions.

So while all of India today uses the same offset of +05:30, that has not always been the case, nor is it likely to always be true in the future (based on the history of how often zones change around the world).

ISO 8601

The ISO 8601 standard defines many sensible formats for representing date-time values as text.

2020-01-23T12:34:56.123456789+05:30

The java.time framework built into Java 8 and later extends one of those format wisely by appending the name of the time zone in square brackets. I suggest using this format if feasible.

2020-01-23T12:34:56.123456789+05:30[Asia/Kolkata]


来源:https://stackoverflow.com/questions/60790342/is-there-any-way-by-which-i-can-get-timezone-value-using-time-stamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!