如何在 groovy 中创建动态日期生成器

How to create a dynamic date generator in groovy

我有一个简单的日期生成器代码,其中 select 是从现在到 120 天的日期。

但是,我希望我的约会更聪明一点。我只想从 五月、六月、七月和八月 开始 select。

因此,如果当前日期是 5 月 之前,那么 select 是这四个月之一内的随机日期今年。
否则,如果当前日期是 6 月之后,则 select 明年 4 个月中任何一个的日期。

groovy怎么写?

下面是当前日期生成器。

//Defines Start and End Dates for holiday departure date search
Random random = new Random();
date = new Date()
def startDate = date + random.nextInt(100 + 120)
sDate = startDate.format("yyyy-MM-dd")

testRunner.testCase.testSuite.setPropertyValue('arrivaldate', sDate);

在 op 澄清后编辑)

def getRandomDate(Date now, Random random) {
  def year         = now.year + (now.month > 5 ? 1 : 0)
  def allDates     = (new Date(year, 4, 1)..new Date(year, 7, 31))
  def allowedDates = allDates.findAll { now < it }

  allowedDates[random.nextInt(allowedDates.size())]
}

// example usage
def now    = new Date()
def random = new Random()

100.times { 
  def today      = now + it*3
  def randomDate = getRandomDate(now+it*3, random)
  println "today: ${today.format('yyyy MMM dd')} ==> random date: ${randomDate.format('yyyy MMM dd')}"
}

上面的执行会打印出如下内容:

today: 2017 Feb 25 ==> random date: 2017 Jul 21
today: 2017 Feb 28 ==> random date: 2017 Aug 03
today: 2017 Mar 03 ==> random date: 2017 Jun 21
today: 2017 Mar 06 ==> random date: 2017 May 21
today: 2017 Mar 09 ==> random date: 2017 Jun 05
today: 2017 Mar 12 ==> random date: 2017 May 03
today: 2017 Mar 15 ==> random date: 2017 Aug 04
today: 2017 Mar 18 ==> random date: 2017 Aug 17
today: 2017 Mar 21 ==> random date: 2017 Jul 09
today: 2017 Mar 24 ==> random date: 2017 Jul 11
today: 2017 Mar 27 ==> random date: 2017 Jul 16
today: 2017 Mar 30 ==> random date: 2017 Aug 09
today: 2017 Apr 02 ==> random date: 2017 Jul 09
today: 2017 Apr 05 ==> random date: 2017 Aug 05
today: 2017 Apr 08 ==> random date: 2017 Jul 19
today: 2017 Apr 11 ==> random date: 2017 Aug 10
today: 2017 Apr 14 ==> random date: 2017 Jun 21
today: 2017 Apr 17 ==> random date: 2017 Aug 03
today: 2017 Apr 20 ==> random date: 2017 May 02
today: 2017 Apr 23 ==> random date: 2017 Jul 04
today: 2017 Apr 26 ==> random date: 2017 Jun 13
today: 2017 Apr 29 ==> random date: 2017 May 02
today: 2017 May 02 ==> random date: 2017 Aug 01
today: 2017 May 05 ==> random date: 2017 Aug 07
today: 2017 May 08 ==> random date: 2017 Aug 20
today: 2017 May 11 ==> random date: 2017 Jun 29
today: 2017 May 14 ==> random date: 2017 May 18
today: 2017 May 17 ==> random date: 2017 Jun 11
today: 2017 May 20 ==> random date: 2017 May 26
today: 2017 May 23 ==> random date: 2017 Jul 06
today: 2017 May 26 ==> random date: 2017 Aug 29
today: 2017 May 29 ==> random date: 2017 Jun 02
today: 2017 Jun 01 ==> random date: 2017 Jun 09
today: 2017 Jun 04 ==> random date: 2017 Jun 07
today: 2017 Jun 07 ==> random date: 2017 Aug 09
today: 2017 Jun 10 ==> random date: 2017 Aug 02
today: 2017 Jun 13 ==> random date: 2017 Aug 04
today: 2017 Jun 16 ==> random date: 2017 Aug 09
today: 2017 Jun 19 ==> random date: 2017 Jun 22
today: 2017 Jun 22 ==> random date: 2017 Aug 16
today: 2017 Jun 25 ==> random date: 2017 Aug 13
today: 2017 Jun 28 ==> random date: 2017 Jul 05
today: 2017 Jul 01 ==> random date: 2018 Jun 18
today: 2017 Jul 04 ==> random date: 2018 Jul 29
today: 2017 Jul 07 ==> random date: 2018 Jul 13
today: 2017 Jul 10 ==> random date: 2018 Jul 26
today: 2017 Jul 13 ==> random date: 2018 Aug 23
today: 2017 Jul 16 ==> random date: 2018 Aug 12
today: 2017 Jul 19 ==> random date: 2018 Aug 24
today: 2017 Jul 22 ==> random date: 2018 Aug 20
today: 2017 Jul 25 ==> random date: 2018 Jul 28
today: 2017 Jul 28 ==> random date: 2018 Jul 29
today: 2017 Jul 31 ==> random date: 2018 May 02
today: 2017 Aug 03 ==> random date: 2018 Jul 19
today: 2017 Aug 06 ==> random date: 2018 Aug 05
today: 2017 Aug 09 ==> random date: 2018 Aug 28
today: 2017 Aug 12 ==> random date: 2018 Jul 16
today: 2017 Aug 15 ==> random date: 2018 Aug 04
today: 2017 Aug 18 ==> random date: 2018 May 30
today: 2017 Aug 21 ==> random date: 2018 May 02
today: 2017 Aug 24 ==> random date: 2018 May 01
today: 2017 Aug 27 ==> random date: 2018 May 10
today: 2017 Aug 30 ==> random date: 2018 May 04
today: 2017 Sep 02 ==> random date: 2018 Jun 30
today: 2017 Sep 05 ==> random date: 2018 May 05
today: 2017 Sep 08 ==> random date: 2018 Jul 27
today: 2017 Sep 11 ==> random date: 2018 Aug 14
today: 2017 Sep 14 ==> random date: 2018 Jul 15
today: 2017 Sep 17 ==> random date: 2018 Jul 12
today: 2017 Sep 20 ==> random date: 2018 Jul 24
today: 2017 Sep 23 ==> random date: 2018 Aug 28
today: 2017 Sep 26 ==> random date: 2018 Jul 26
today: 2017 Sep 29 ==> random date: 2018 Jun 27
today: 2017 Oct 02 ==> random date: 2018 Aug 15
today: 2017 Oct 05 ==> random date: 2018 Jun 27
today: 2017 Oct 08 ==> random date: 2018 Jun 01
today: 2017 Oct 11 ==> random date: 2018 Jun 12
today: 2017 Oct 14 ==> random date: 2018 Jun 06
today: 2017 Oct 17 ==> random date: 2018 Aug 02
today: 2017 Oct 20 ==> random date: 2018 May 05
today: 2017 Oct 23 ==> random date: 2018 Jun 15
today: 2017 Oct 26 ==> random date: 2018 Jun 05
today: 2017 Oct 29 ==> random date: 2018 Aug 12
today: 2017 Nov 01 ==> random date: 2018 Aug 29
today: 2017 Nov 04 ==> random date: 2018 May 15
today: 2017 Nov 07 ==> random date: 2018 Jul 03
today: 2017 Nov 10 ==> random date: 2018 Aug 16
today: 2017 Nov 13 ==> random date: 2018 Aug 21
today: 2017 Nov 16 ==> random date: 2018 May 06
today: 2017 Nov 19 ==> random date: 2018 Jul 15
today: 2017 Nov 22 ==> random date: 2018 Jul 03
today: 2017 Nov 25 ==> random date: 2018 Aug 15
today: 2017 Nov 28 ==> random date: 2018 Jul 29
today: 2017 Dec 01 ==> random date: 2018 May 06
today: 2017 Dec 04 ==> random date: 2018 Aug 31
today: 2017 Dec 07 ==> random date: 2018 May 12
today: 2017 Dec 10 ==> random date: 2018 May 01
today: 2017 Dec 13 ==> random date: 2018 Aug 31
today: 2017 Dec 16 ==> random date: 2018 Jul 17
today: 2017 Dec 19 ==> random date: 2018 Jul 24

本质上,我们从允许的日期列表中随机选择一个日期。

不在每次调用时都创建 allowedDates 列表可以提高性能。

某些日期方法已弃用。如果弃用很重要,您可能希望改用 Calendar。

groovy 范围 .. 日期运算符 return 日期范围包含两个给定日期之间的所有日期,包括两个给定日期。

我将 now 留在方法外部以使测试更容易,并且我将 random 留在方法外部,因为我认为在每次调用时重新创建新的随机实例对生成的随机分布的质量。

注1:对于5月1日至6月30日之间的日期,问题定义不明确。对于这些日期,代码 return 是当年的随机日期,不包括过去的日期。 IE。在 6 月 15 日,该算法将 return 从今年的 6 月 16 日到 8 月 31 日之间的随机日期。 7 月 1 日是 return 明年的随机日期,介于 5 月 1 日和 8 月 31 日之间。

注 2: 没有弃用日期调用和使用日历的替代版本:

import static java.util.Calendar.*
import java.util.GregorianCalendar as Cal

def getRandomDate(Date now, Random random) {
  def cal = new Cal(time: now)

  // use current year if current date is before july, otherwise next year
  def year         = cal.get(YEAR) + (cal.get(MONTH) > 5 ? 1 : 0)
  def allDates     = (new Cal(year, 4, 1).time..new Cal(year, 7, 31).time)
  def allowedDates = allDates.findAll { now < it }

  allowedDates[random.nextInt(allowedDates.size())]
}

这里是 Groovy 脚本,它可以执行您想要执行的操作。

请注意,如果当前日期落在问题中缺少的日期范围内,下面的脚本将抛出错误。

Not implemented for the days between 1st May to 30th June

请关注内联评论。

import groovy.time.TimeCategory

def dateFormat = 'yyyy-MM-dd'

def getNumberInRange = { min, max -> new Random().nextInt(max + 1 - min) + min }

def isTodayBeforeMay = { Calendar.MONTH < 5 }

def isTodayAfterJune = { Calendar.MONTH > 6 }

//Get the number of days between today and given date
def getDifferenceDays = { targetDate, closure ->
    def strDate = closure (targetDate)
    def futureDate = new Date().parse(dateFormat, strDate)
    TimeCategory.minus(futureDate, new Date()).days
}

//Get the offset between today and max date i.e.,31 august
def getOffSetDays = { date ->
    //Need to change the date range if needed.
    //As per OP, May to August is mentioned below
    def max = getDifferenceDays(date) { "${it[Calendar.YEAR]}-08-31" }
    def min = getDifferenceDays(date) { "${it[Calendar.YEAR]}-05-01" }
    getNumberInRange(min, max)
}


def now = new Date()
def nextYearNow = now.updated(year: now[Calendar.YEAR] + 1)

def selected
def finalDate

println "Today : $now"
println "Next year same date : $nextYearNow"

if (isTodayBeforeMay()) {
    selected = now    
} else if (isTodayAfterJune()) {
    selected = nextYearNow
} else {
    //It is not mentioned what should happened for the mentioned period by OP
    throw new Error("Not implemented for the days between 1st May to 30th June")
}

def offset = getOffSetDays(selected)

//Add the offset days to now
use(TimeCategory) {
    finalDate = now + offset.days
}
println "Final future date is : $finalDate"
println "Final future date is(formatted) : ${finalDate.format(dateFormat)}"

//set the date at test case level property
context.testCase.setPropertyValue('NEXT_DATE', finalDate.format(dateFormat))

为了得到评估的日期

  • 在请求中使用 ${#TestCase#NEXT_DATE}
  • 在groovy脚本中使用context.expand('${#TestCase#NEXT_DATE}')(返回字符串)

注意:如果您想在 soapui 工具中查看输出,可以将上述脚本中的 println 替换为 log.info。否则,打印语句会显示在日志文件中。

您可以在线快速试用以上脚本Demo

输出: