Created
October 20, 2017 01:24
-
-
Save ppazos/365a62547e602fa1007e0c5962f9f84b to your computer and use it in GitHub Desktop.
Generates random date with time between two given dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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