Dipankar Ghosh
Dipankar Ghosh

Reputation: 11

formatting same date in different format using JMeter

Hi I want to format date (05/07/2018 17:04:00) to (20180507) inside jmeter. Please help me.using some sampler. I need to use the date variable to make another JSON call.

Upvotes: 1

Views: 6225

Answers (4)

govardhan nalluri
govardhan nalluri

Reputation: 21

The above code is not working. From the response I am getting as 17-Apr-2020 needs to change as 17/04/2020 and send to the corresponding request.

import java.text.SimpleDateFormat

import java.text.DateFormat;

SimpleDateFormat source = new SimpleDateFormat("dd-MMM-YYYY");
SimpleDateFormat target = new SimpleDateFormat("dd/MM/YYYY");
Date date = source.parse(vars.get("C_Date"));
log.info("Date value="+date);
String newDate = target.format(date);
log.info("newDate value="+newDate);
vars.put("NewDate", newDate);][1]][1]

Upvotes: 0

ararar
ararar

Reputation: 983

Add a BeanShell Sampler with the below code in the code area:

import java.text.SimpleDateFormat;
import java.text.DateFormat;

SimpleDateFormat source = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
SimpleDateFormat target = new SimpleDateFormat("yyyyMMdd");
Date date = source.parse(vars.get("C_ticketcreatedate"));
String newDate = target.format(date);
vars.put("NewDate", newDate);

Now you can use the new date format like this ${NewDate}

enter image description here

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168197

You can do this using any suitable JSR223 Test Element and Groovy language like:

vars.put('date', Date.parse('MM/dd/yyyy HH:mm:ss', '05/07/2018 17:04:00').format('yyyyMMdd'))

After that you can access converted date as ${date} or ${__V(date)} where required

Upvotes: 1

Rohit
Rohit

Reputation: 611

You can use Jmeter __time() function to format the date and time .

in your case use ${__time(yyyymmdd,)} to generate the date

enter image description here

For More information

Jmeter __time Function

You can follow this Blogs for information on JMeter

Upvotes: 0

Related Questions