JMeter – How to Write a BeanShell Code to Get Current Date or Feature Date or Older Date

Spread the love

Here I am going to explain and write a simple snippet that show how you can write a short code in JMeter BeanShell code that pull out the current date or you can modify to get feature date or previuos old date.

This is a simple code.

import java.text.SimpleDateFormat;
import java.util.Date;
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String formattedDate = df.format(date);

Above Code Explanation:

  • The first two lines is defined to get java definations.
  • Next line will get the current system date. <Date date = new Date();>
  • Next line will addition or substract the current date by adding + (plus) or – (minus) number of days future or previous. <date.setDate(date.getDate()+5);> This will add 5 days from the current date.
  • Next line format the current date as per your requirement. <(“dd/MM/yyyy”);> you can format as you like ddMMyyyy, dd-MM-yyyy, read more format dates here.
    • Even you can defind directly below fucntion in JMeter, but not in BeanSheet.
      • ("dd-mmm-yyyy HHmmss") this will get 20-032-2015 233224
      • ("d-MMM-yyyy hhmmss") this will get 20-May-2015 113224
      • ("dd-m-yyyy hhmmss") this will get 20-32-2015 113224
      • ("D-M-yyyy hhmmss") this will get 140-5-2015 113224
      • ("DD-MM-yyyy") this will get 140-05-2015
  • The Next line is format the date as we required.
  • The Final line is put the required date into a variable, that you can parameterized in your JMeter Script with ${date} variable.

I hope you got the required information from this artilce post, If you have any doubts, or questions, you can ask from below comment session, I will explan and give more detailed on this topic.

You May Interest to read How To Clear All Results in JMeter using BeanShell Code

Spread the love

One Reply to “JMeter – How to Write a BeanShell Code to Get Current Date or Feature Date or Older Date”

Leave a Reply

Your email address will not be published. Required fields are marked *