March 7, 2020

Creating Dates in JMeter Using the TimeShift Function

Open Source Automation
Continuous Testing

In the previous article, “Creating and Testing Dates in JMeter - Learn How”, I showed ways to create dates by using the Java code. In this article, we'll talk about how to create dates in Apache JMeter™ using the __timeShift function. Please note that this approach is for demonstrative purposes only, don't do this for performance testing.

In order to create a date using this function, you need to do the following:

1. Add a Thread Group

Right Click -> Add -> Threads(Users) -> Thread Group

creating dates in jmeter

2. Add a JSR223 Sampler

Thread Group -> Right Click -> Add -> Sampler -> JSR223 Sampler

 

JSR223 Sampler -> Language Groovy

using the timeshift function in jmeter

In the JSR223 Sampler, add the following code example.

llonga=${__timeShift(,,,,)};log.info("Current date in millis: "+a);Stringb="${__timeShift(dd/MM/yyyy,,,,)}";log.info("Current date in the specified format: "+b);Stringb1="${__timeShift(y-MM-dd HH:mm:ss,,,,)}";log.info("Current date in the specified format: "+b1);${__timeShift(dd/MM/yyyy,,P1D,,c)};log.info("Tomorrow: "+"${c}");${__timeShift(dd-MM-y-HH-mm-ss,,P1DT3H4M,,c1)};log.info("Tomorrow: "+"${c1}");${__timeShift(dd/MM/yyyy,,P-1D,,c2)};log.info("Yesterday: "+"${c2}");${__timeShift(dd/MM/yyyy,,P365D,,c3)};log.info("Next year: "+"${c3}");"${__timeShift(y MMM dd HH:mm:ss,,,fr_FR, c4)}";log.info("Current date with localization: "+"${c4}");${__timeShift(dd/MM/yyyy,10/10/2010,P365D,,c5)};log.info("Next year: "+"${c5}");

 

timeshift function tutorial, jmeter

The __timeShift (format, date, shift, locale, variable) function has the following parameters:

  • format - the format in which the created date will be displayed. All possible formats that you can use are very well described in the DateTimeFormatter class. If the value is not passed, the date is created in milliseconds.
  • date - It is the date value. Used if you want to create a specific date by adding or subtracting a certain number of days, hours or minutes. If the parameter value is not passed, then the current date is used.
  • shift - it is a value that indicates how many days, hours, or minutes to add or subtract from the value of the date parameter. If the value is not passed, nothing is subtracted or added to the value of the date parameter. The possible values are described below:
    • "PT20.345S" parses as "20.345 seconds"
    • "PT15M" parses as "15 minutes"
    • "PT10H" parses as "10 hours"
    • "P2D" parses as "2 days"
    • "P2DT3H4M" parses as "2 days, 3 hours and 4 minutes"
    • "P-6H3M" parses as "-6 hours and +3 minutes"
    • "-P6H3M" parses as "-6 hours and -3 minutes"
    • "-P-6H+3M" parses as "+6 hours and -3 minutes"
  • locale - sets the language in which the created date will be displayed. All possible values are described on the Oracle website. If the value is not passed, then the value that is set JMeter is used.
  • variable - the name of the variable to which the value of the created date will be assigned. Parameter not required.

 

Knowing the parameters of the __timeShift function, the code shown above does the following:

long a = ${__timeShift(,,,,)}; - Creates the current date in milliseconds and assigns the resulting value to the variable "a" with the data type long

log.info("Current date in millis: " + a); - Displays the created date in the the JMeter console

String b = "${__timeShift(dd/MM/yyyy,,,,)}"; - Creates the current date, in the specified format, and assigns the resulting value to the variable "b" with the data type String.

${__timeShift(dd/MM/yyyy,,P1D,, c)}; - Creates the current date, in the specified format, plus one day. "C" is the name of the variable to which the value is assigned.

${__timeShift(dd-MM-y-HH-mm-ss,,P1DT3H4M,, c1)}; - Creates the current date, in the specified format, plus one day, 3 hours and 4 minutes. "C1" is the name of the variable to which the value is assigned.

${__timeShift(dd/MM/yyyy,,P-1D,, c2)}; - Creates the current date, in the specified format, minus one day. "C2" is the name of the variable to which the value is assigned.

${__timeShift(dd/MM/yyyy,,P365D,, c3)}; - Creates the current date, in the specified format, plus 365 days. "C3" is the name of the variable to which the resulting value is assigned.

"${__timeShift(y MMM dd HH:mm:ss,,,fr_FR, c4)}"; - Creates the current date in the specified format that will be displayed in French.

${__timeShift(dd/MM/yyyy,10/10/2010,P365D,, c5)}; - Creates a date on 10/10/2010 plus 365 days.

Before running the code described above, you need to open the JMeter console, where the result will be displayed.

how to use the jmeter timeshift function to create dates

The image above shows the created dates in different formats.

Witness the harmoney of JMeter and BlazeMeter firsthand — get started with the industry's most trusted performance testing platform for FREE today!

Start Testing Now

Running Your Tests in BlazeMeter

When you finish creating your code and creating your script, you can upload your JMX file to BlazeMeter. In BlazeMeter, you can scale the number of users and run your script from all over the world. Then, you can analyze results in reports that show KPIs in real-time and over time, and you can share the reports with team members or non-technical audiences.

load testing with dates

START TESTING NOW

 

Related Resources: