Skip to content

Instantly share code, notes, and snippets.

@ppazos
Created October 20, 2017 01:24
Show Gist options
  • Save ppazos/365a62547e602fa1007e0c5962f9f84b to your computer and use it in GitHub Desktop.
Save ppazos/365a62547e602fa1007e0c5962f9f84b to your computer and use it in GitHub Desktop.
Generates random date with time between two given dates
import groovy.time.TimeCategory
random = new Random()
Date randomDate(Range<Date> range) {
def res = range.from + random.nextInt(range.to - range.from + 1)
use( TimeCategory ) {
res = res + random.nextInt(24).hours + random.nextInt(60).minutes + random.nextInt(60).seconds
}
res
}
def start = Date.parse('yyyy-MM-dd mm:ss', '2015-01-01 00:00')
def end = Date.parse('yyyy-MM-dd mm:ss', '2017-12-31 23:59')
println randomDate(start..end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment