How do I set up custom date range using JAVA API

966 views
Skip to first unread message

Tom

unread,
Mar 10, 2014, 8:33:44 PM3/10/14
to adwor...@googlegroups.com
Hi ,
 
I am new to adwords. I am trying to use custom date range using JAVA API and I found on forums that date range is a property of a selector and found this about stats objects http://googleadsdeveloper.blogspot.com/2013/10/adwords-api-how-to-migrate-from-stats.html.
So I am trying:
 
Calendar cal = Calendar.getInstance();
Selector selector = new Selector();
String startdate = new SimpleDateFormat("20130101").format(cal.getTime());
String enddate = new SimpleDateFormat("20131231").format(cal.getTime());
selector.setDateRange(new DateRange(startdate, enddate));
 
but its throwing an error:
 
The method setDateRange(com.google.api.ads.adwords.lib.jaxb.v201309.DateRange) in the type Selector is not applicable for the arguments (com.google.api.ads.adwords.axis.v201306.cm.DateRange)
 
I am using client library version v201309.
 
Please let me know where am I erring.
 
Thanks for help !

Josh Radcliff (AdWords API Team)

unread,
Mar 11, 2014, 10:27:58 AM3/11/14
to adwor...@googlegroups.com
Hi,

The exception below indicates that you simply imported the wrong DateRange class.  If you are using Axis then you'll want to import the following:

com.google.api.ads.adwords.axis.v201309.cm.DateRange

As an aside, your constructor call for SimpleDateFormat looks incorrect.  You should pass a string reflecting the desired format rather than the date itself.  My recommendation would be to switch to using Joda time's DateTime class instead, as shown in the blog post examples.  The Joda API is much simpler and avoids some of the pitfalls of the standard Date and DateFormat classes.

Cheers,
Josh, AdWords API Team

Tom

unread,
Mar 11, 2014, 2:53:11 PM3/11/14
to adwor...@googlegroups.com
Thanks for the reply Josh !
 
I imported com.google.api.ads.adwords.axis.v201309.cm.DateRange class and getting the below error:
 

The method setDateRange(com.google.api.ads.adwords.lib.jaxb.v201309.DateRange) in the type Selector is not applicable for the arguments (com.google.api.ads.adwords.axis.v201309.cm.DateRange)

Josh Radcliff (AdWords API Team)

unread,
Mar 11, 2014, 5:10:59 PM3/11/14
to adwor...@googlegroups.com
Hi Tom,

Sorry, I misread the original error.  Please remove any imports of DateRange that you currently have and replace them with:

import com.google.api.ads.adwords.lib.jaxb.v201309.DateRange;

Thanks,
Josh, AdWords API Team

Tom

unread,
Apr 2, 2014, 8:07:21 PM4/2/14
to adwor...@googlegroups.com
Hi ,
 
Picking up the old thread...I am using
 
    Selector selector = new Selector();

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date startdate = formatter.parse("2014-04-01");
    java.util.Date enddate = formatter.parse("2014-04-02");
    selector.setDateRange(new DateRange(startdate,enddate));

But getting an error :

The constructor DateRange(Date, Date) is undefined

I am importing com.google.api.ads.adwords.lib.jaxb.v201309.DateRange . So not exactly sure what am I doing wrong.

Any help is appriciated.

Josh Radcliff (AdWords API Team)

unread,
Apr 3, 2014, 9:35:54 AM4/3/14
to adwor...@googlegroups.com
Hi Tom,

Below is a sample of how to pass a custom date range.

    DateRange dateRange = new DateRange();
    dateRange.setMin("20140101");
    dateRange.setMax("20140131");
    selector.setDateRange(dateRange);

Are you using an IDE such as Eclipse?  It would provide you with context assistance and let you know the valid constructor calls and data types.

Cheers,
Josh, AdWords API Team

Tom

unread,
Apr 3, 2014, 4:10:18 PM4/3/14
to adwor...@googlegroups.com
It worked for me. Thanks Josh !
Reply all
Reply to author
Forward
0 new messages