Skip to content

Instantly share code, notes, and snippets.

@xnrghzjh
Created June 10, 2011 04:15
Show Gist options
  • Save xnrghzjh/1018215 to your computer and use it in GitHub Desktop.
Save xnrghzjh/1018215 to your computer and use it in GitHub Desktop.
指定範囲内の日付をランダムに取得(重複選択可)
class DateRange {
def range
def index = 0
DateRange (f, t) {
range = (toDate(f)..toDate(t)).toList().with {
Collections.shuffle(it); it
}
}
// 変換処理
def toDate = { Date.parse('yyyy-MM-dd', it) }
def toDateStr = { it.format('yyyy-MM-dd') }
// ランダムに取得 d = 重複許可
def ramdomChoice(d = false) {
shuffle(d)
toDateStr(range.get(index++))
}
// シャッフル
def shuffle = { if(it) {Collections.shuffle(range); index = 0 }}
}
// 実行
dateRange = new DateRange('2011-06-01', '2011-06-05')
println '重複を禁止'
5.times {
println dateRange.ramdomChoice()
}
println '重複を許可'
5.times {
println dateRange.ramdomChoice(true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment