Search code examples
datejmetertimestampincrement

JMeter: How to get date of last week starting from sunday till saturday?


The scenario is I need to get last week date in format yyyy-mm-dd HH:mm:ss from last Sunday till Saturday(date will increment by 1 from last week of Sunday till Saturday) and like wise one more is to get date from past 20 weeks(till last week).

How may I get it?


Solution

  • Your requirement is not very clear so I can provide only a generic solution:

    1. Getting the "last Sunday"

       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. Getting all previous "dates" for last 20 weeks:

       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

    More information: