0

Scenario : I need the find the current date and time for the Latitude and longitude in apex future method. I can able to pass latitude and longitude and getting the timezone name, timezone id, raw offset and dst offset but I dont know how to get the Date and Time for the location.

@future(callout=true)
    public static void getAssetLocalTimeAtSubmit(string Latitude, string Longitude){
        //String geocodingKey = X4HourDepotCalculationSetting__c.getInstance().GoogleServerKey__c;
        String endpoint = 'https://maps.googleapis.com/maps/api/timezone/xml?location=' +Latitude+ ',' +Longitude+'&timestamp=1331161200&key='+geocodingKey;
        Http h = new Http();
        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('GET');

        // Send the request, and return a response
        HttpResponse res = h.send(req); 

        System.debug('Respnse body '+res.getBody());
        Dom.Document doc = new Dom.Document();
        doc.load(res.getBody());

        Dom.XMLNode xroot = doc.getRootElement();
        string TimeZone = xroot.getChildElement('time_zone_name', null).getText();
        System.debug('TimeZone: ' + TimeZone);

            **How do I find the Actual Date and Time here?**

    }

Response body

<TimeZoneResponse>
 <status>OK</status>
 <raw_offset>19800.0000000</raw_offset>
 <dst_offset>0.0000000</dst_offset>
 <time_zone_id>Asia/Calcutta</time_zone_id>
 <time_zone_name>India Standard Time</time_zone_name>
</TimeZoneResponse>
0

Once you have a time zone id (Asia/Calcutta) you can use the Apex TimeZone class to do the rest:

TimeZone timeZone = TimeZone.getTimeZone(timeZoneId);
Datetime now = Datetime.now();
Datetime targetDateTime = now.addSeconds(timeZone.getOffset(now));
  • We cannot use calendar class directly in apex. It is showing Invalid type: GregorianCalendar – Prince Dec 1 '16 at 6:50
  • Oops, edited my answer, the above should work for you – citizen conn Dec 1 '16 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.