Skip to content
Advertisement

Calculating timezone from GMT value – Android


In a android form , i am accepting a GMT value(offset) from user such a +5:30 , +3:00.
and from this value , i want to calculate the timeZone that is “India/Delhi”.

Any ideas on how to do it ……Plzz

Advertisement

Answer

If you already have a specific instant in time at which that offset is valid, you could do something like this:

JavaScript

On my box that prints:

JavaScript

(As far as I’m aware, India/Delhi isn’t a valid zoneinfo ID.)

If you don’t know an instant at which the offset is valid, this becomes rather harder to really do properly. Here’s one version:

JavaScript

… but that assumes that there are only ever two offsets per time zone, when in fact time zones can change considerably over history. It also gives you a much wider range of IDs, of course. For example, an offset of one hour would include both Europe/London and Europe/Paris, because in summer time London is at UTC+1, whereas in winter Paris is at UTC+1.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement