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.

    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();

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

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

        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

 <time_zone_name>India Standard Time</time_zone_name>

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.