Test
Test

Reputation: 49

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?

Upvotes: 0

Views: 705

Answers (1)

Dmitri T
Dmitri T

Reputation: 168147

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:

Upvotes: 1

Related Questions