Hi,
The code for your class had some bugs in it.
Below is a simple implementation of a class that achieves what you want...
package groovyScripts.play; // Change this to your package name.
class DOB {
//Define our range.
def start = Date.parse('yyyy-MM-dd', '2015-01-01')
def end = Date.parse('yyyy-MM-dd', '2017-12-31')
def random = new Random();
def DOB() {
// Constructor for the object.
// nothing to do here, but constructors are useful for one-time set up tasks.
}
Date randomDate() {
// This is the method we will call to get a random date.
Range<Date> range = (this.start..this.end);
def res = range.from + random.nextInt(range.to - range.from + 1)
return res;
}
}
Then, in your groovy script, you can create the object and call the mehtod...
def dobObject = new groovyScripts.play.DOB(); // remember to update the package
log.info(dobObject.randomDate());
Here's a more refined example of the class, which uses the constructor to set-up the object..
package groovyScripts.play;
class DOB {
//Define our properties.
def start = null;
def end = null;
def random = null;
Range<Date> range = null;
def DOB() {
// Constructor for the object.
// Let's set up what we can up front.
this.start = Date.parse('yyyy-MM-dd', '2015-01-01')
this.end = Date.parse('yyyy-MM-dd', '2017-12-31')
this.random = new Random();
this.range = (this.start..this.end);
}
Date randomDate() {
def res = this.range.from + this.random.nextInt(this.range.to - this.range.from + 1)
return res;
}
}
You can even 'overload' the constructor so that you can pass in the range, should you want to override the defaults....
Here's the updated class...
package groovyScripts.play;
class DOB {
//Define our range.
def start = null;
def end = null;
def random = null;
Range<Date> range = null;
def DOB() {
// Constructor for the object.
// Let's set up what we can up front.
this.start = Date.parse('yyyy-MM-dd', '2015-01-01')
this.end = Date.parse('yyyy-MM-dd', '2017-12-31')
this.random = new Random();
this.range = (this.start..this.end);
}
def DOB(start, end) {
// Overloaded Constructor for the object.
// Let's set up the object with passed in values.
this.start = Date.parse('yyyy-MM-dd', start);
this.end = Date.parse('yyyy-MM-dd', end);
this.random = new Random();
this.range = (this.start..this.end);
}
Date randomDate() {
def res = this.range.from + this.random.nextInt(this.range.to - this.range.from + 1)
return res;
}
}
Usage example....
// Use the defaults in the class....
def dobObject = new groovyScripts.play.DOB();
log.info(dobObject.randomDate());
// Let's override the defaults....
def dobObject2 = new groovyScripts.az.play.DOB('1990-01-01', '1990-12-31');
log.info(dobObject2.randomDate());