-
-
Save fumokmm/1016448 to your computer and use it in GitHub Desktop.
指定範囲内の日付をランダムに取得
This file contains 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
@Grab(group='joda-time', module='joda-time', version='*') | |
import org.joda.time.* | |
//def rand_date(def fr, def to) { // java... | |
def rand_date = {fr,to-> // groovy!! | |
def d = new DateTime(fr) | |
def range = Days.daysBetween(d, new DateTime(to)).getDays() + 1 | |
d.plusDays((Integer)Math.floor(Math.random() * range)).toString("yyyy-MM-dd") | |
} | |
5.times { | |
println rand_date("1978-09-30","2011-10-10") | |
} |
This file contains 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
// 範囲内ランダム | |
ObjectRange.metaClass.randomeChoice = { | |
delegate.toList().with { | |
Collections.shuffle(it); it | |
}.head() | |
} | |
// 変換 | |
def toDate = { Date.parse('yyyy-MM-dd', it) } | |
def toDateStr = { it.format('yyyy-MM-dd') } | |
// 日付の範囲 | |
def dateRange = toDate('1978-09-30')..toDate('2011-10-10') | |
// デモ! | |
5.times { println toDateStr(dateRange.randomeChoice()) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment