JMeter:如何从星期日到星期六获取上周的日期?

时间:2021-05-02 03:56:35

标签: date jmeter timestamp increment

场景是我需要从上周日到周六以 yyyy-mm-dd HH:mm:ss 格式获取上周日期(日期将从周日到周六的最后一周增加 1),同样明智的还有一个获取过去 20 周(直到上周)的日期。

我怎样才能得到它?

1 个答案:

答案 0 :(得分:1)

您的要求不是很清楚,所以我只能提供一个通用的解决方案:

  1. 获取“最后一个星期天”

     def calendar = Calendar.instance
     def delta = Calendar.SUNDAY - calendar.get(Calendar.DAY_OF_WEEK)
     calendar.add(Calendar.DAY_OF_WEEK, delta)
     log.info('Last Sunday: ' + calendar.time.format("yyyy-MM-dd HH:mm:ss"))
    

    enter image description here

  2. 获取过去 20 周的所有“日期”:

     def now = new Date()
     use(groovy.time.TimeCategory) {
         def twentyWeeksAgo = now - 30.weeks
         def duration = now - twentyWeeksAgo
         1.upto(duration.days, {
             twentyWeeksAgo = twentyWeeksAgo + 1.days
             log.info(twentyWeeksAgo.format('yyyy-MM-dd HH:mm:ss'))
         })
     }
    

    enter image description here

更多信息: