How to convert Google time zones

142 views
Skip to first unread message

dongfu li

unread,
Jun 4, 2023, 2:16:20 AM6/4/23
to Google Ads API and AdWords API Forum
Hello,

When I call the changeStatus interface using the Google ads API, I need to pass a 'last_change_date_time' Time parameter,
Based on my understanding,
This time is actually an account time zone, so I need to convert our time in Shanghai to this account time zone. When I used JAVA code to convert, most of the time zones were fine, but some of the time zones were converted to deviate from your time zone. For example, in the Africa/Cairo time zone, the time difference between our conversion and Shanghai time difference is 6 hours, while the time difference returned by your interface is 5 hours. How can we solve this problem? Thanks !

Additionally, I am attaching the converted JAVA code:

@SneakyThrows
public static String timeZoneTransferShanghai(String time, String nowTimeZone,String targetTimeZone) {
if(StringUtils.isBlank(time)){
return "";
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone(nowTimeZone));
Date sourceDate = formatter.parse(time);
formatter.setTimeZone(TimeZone.getTimeZone(targetTimeZone));
String targetDate = formatter.format(sourceDate);
return targetDate;
}

@Test
public void timeZoneTest(){
String currentTime = DateUtils.toString(new Date()) ;
String targetTime = timeZoneTransferShanghai(currentTime,"Asia/Shanghai","Africa/Cairo") ;
System.out.println("Shanghai time is :"+currentTime+" ; Cairo time is: "+targetTime);
}


result print: 
Shanghai time is :2023-06-04 10:01:13 ;  Cairo time is:  2023-06-04 04:01:13


Google Ads API Forum Advisor

unread,
Jun 7, 2023, 9:30:18 PM6/7/23
to dong...@meetsocial.com, adwor...@googlegroups.com

Hi there,

Thanks for reaching out to the Google Ads API Forum.

Yes, Google Ads API pulls the account timezone. You may see if below sample code which will first get the current time in the Asia/Shanghai timezone using the SimpleDateFormat class. Then, it will set the timezone of the SimpleDateFormat object to the Africa/Cairo timezone. Finally, it will parse the time in the Asia/Shanghai timezone to get the time in the Africa/Cairo timezone.

@SneakyThrows

public static String timeZoneTransferShanghai(String time, String nowTimeZone,String targetTimeZone) {

        if(StringUtils.isBlank(time)){

        return "";

         }

        TimeZone shanghaiTimeZone = TimeZone.getTimeZone("Asia/Shanghai");

        TimeZone cairoTimeZone = TimeZone.getTimeZone("Africa/Cairo");



        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        sdf.setTimeZone(shanghaiTimeZone);



        Date shanghaiTime = new Date();

        String shanghaiTimeStr = sdf.format(shanghaiTime);



        sdf.setTimeZone(cairoTimeZone);



        Date cairoTime = sdf.parse(shanghaiTimeStr);

        String cairoTimeStr = sdf.format(cairoTime);



        System.out.println("The time in Asia/Shanghai is " + shanghaiTimeStr);

        System.out.println("The time in Africa/Cairo is " + cairoTimeStr);

        return cairoTimeStr;

}

 

You may note that Google Ads API pulls the data shown on Ads UI, and converting from one timezone to another is a custom implementations. If you need further assistance, then you may seek through other public forums like Stackoverflow

This message is in relation to case "ref:_00D1U1174p._5004Q2m9BqZ:ref"

Thanks,
 
Google Logo Google Ads API Team

 



Reply all
Reply to author
Forward
0 new messages