Find offset in GMT +- format using latitudes and longitudes.

For instance for Bangalore we can say its GMT+5:30, and in order to know how this is derived, we should know something called ‘TimeZone’

Our globe divides into multiple time zones and for India, either its Bangalore or Mumbai, its GMT + 5:30 as it falls under “Asia/Kolkata” timezone

Check your self for all timezones -> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

When it comes to cities within US, it has many timezones, time in Los Angeles is different from time in New York.

Geographically time zones vary dependending on latitudes and logitudes.

Many longitudes passes through a country, different times it will contain.

Also one important point to note here is when we google about say lat / lon of Bangalore it gives 12.9716° N, 77.5946° E

When we pass it to java program we have to consider below things

For North(denoted as N) and East(denoted as E) include positive sign (+)
For South(denoted as S) and West(denoted as W) include positive sign (-)

So above latitude and longitude translates as (12.9716, 77.5946)

Sao Paulo latitude and longitudes are 23.5505° S, 46.6333° W and translates as (-23.5505, -46.6333)

Now coming to code part, follow below steps, as Java doesn’t provide this feature yet.

I will discuss two ways here to implement, first one stores all lat lon and cities data in txt file, and which was updated almost 6 years ago, and second one is actively maintained (I prefer).

Option 1: Using Atlas project

Step 1 : Fetch timezone from lat / lon

Clone github repository https://github.com/bkiers/atlas

It contains extensive lat / lon mapped to different time zones, and works pretty fast.

Step 2: Simple java program

Create App.java as below

package atlas;

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class App {
    public static void main(String[] args) {

        //Dubai -> GMT+4:00
        double lat = 25.204;
        double lng = 55.2708;

        City city = new Atlas().find(lat, lng);
        System.out.println("City -> "+city.name);
        System.out.println("TimeZone -> "+city.timeZone);

        ZoneId zoneId = ZoneId.of(city.timeZone);
        ZonedDateTime zdt = ZonedDateTime.now( zoneId ) ;
        ZoneOffset offset = zdt.getOffset();

        int offsetInMillis = offset.getTotalSeconds();
        System.out.println("The Offset Value in millis is:" + offsetInMillis);
        System.out.println("Local time is : GMT" + offset);
    }

Running above program produces below output.

City -> Dubai
TimeZone -> Asia/Dubai
The Offset Value in millis is:14400
Local time is : GMT+04:00

And the best part is as we are using java.time package, day light saving is taken care of.

Option 2: This github is maintained actively and you can have a look at code base here -> https://github.com/RomanIakovlev/timeshape

Step 1: Add below dependency to your maven

(In case of maven)

    <dependency>
      <groupId>net.iakovlev</groupId>
      <artifactId>timeshape</artifactId>
      <version>2020a.10</version>
    </dependency>

Step 2: Create App.java as below

package atlas;

import net.iakovlev.timeshape.TimeZoneEngine;

import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Optional;

public class App {
    public static void main(String[] args) {
        TimeZoneEngine engine = TimeZoneEngine.initialize();

        Optional<ZoneId> maybeZoneId = engine.query(25.204, 55.2708);
        ZonedDateTime zdt = ZonedDateTime.now( maybeZoneId.get() ) ;
        ZoneOffset offset = zdt.getOffset();

        int offsetInMillis = offset.getTotalSeconds();
        System.out.println("The Offset Value in millis is:" + offsetInMillis);
        System.out.println("Local time is : GMT" + offset);

    }
}

And above program produces below output.

The Offset Value in millis is:14400
Local time is : GMT+04:00

Leave a Reply

Your email address will not be published. Required fields are marked *

three × four =