0

I am adding days to the current date but I am getting an invalid date in the result.

I am using BeanShellPreprocessor in Jmeter and using following code to get the current date.

import java.text.SimpleDateFormat; 
import java.util.Date; 
Date date = new Date(); 
date.setDate(date.getDate()+15); 
SimpleDateFormat df = new SimpleDateFormat("MM/DD/YYYY HH:mm a"); 
String formattedDate = df.format(date); 
vars.put("slotFirstCall",formattedDate); 

This code is adding 15 days to the current date but the value I am getting in the variable is 02/33/2018. And it is showing a different date on the calendar. Date on the calendar, where I am using it is 03/05/2018.

If I use this code in Java I get the correct result, but in Jmeter I get the invalid date.

1
  • 1
    D (capitalised) means day-in-year. Today is 18th day of the year, so 18+15=33. You probably need "d" which means day of the month.
    – Alexey R.
    Jan 18, 2018 at 13:10

3 Answers 3

2

The code you have provided unlikely implements what you expect. If you refer to SimpleDateFormat specification you will realise that D capitalised represents day of the year but not the day of the month.

Today is 18th day of the year, so 18 + 15 = 33 which does not look surprisingly.

However putting the year day "index" along with the month does not make a lot of sense and does not correspond to any known date-representation format. This makes me think you have a bug in your code.

Change your code to

import java.text.SimpleDateFormat; 
import java.util.Date; 
Date date = new Date(); 
date.setDate(date.getDate()+15); 
SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY HH:mm a"); 
String formattedDate = df.format(date); 
vars.put("slotFirstCall",formattedDate);  

this could help.

UPD:

P.S. - It is also worth checking other specificators in your format. Revise if you really need a at the end of your date pattern since a means AM/PM, however you are using HH which represents the time in 24H format. So AM/PM does not introduce any additional value to your timestampt.

1
  • Please see also my P.S. update
    – Alexey R.
    Jan 18, 2018 at 13:24
0

This is really a Java-related question as opposed to a JMEter question.

That said, this link shows you how to use the Java Date classes you're using and how to manipulate the date.

Mykong Link

The Mykong site is a great source on Java.

2
  • Adams, The code is working fine in case of java but when I am using the same in Jmeter, showing a different result.
    – Anil Kumar
    Jan 18, 2018 at 12:19
  • @AnilKumar - I edited your question to add this information.
    – Kate Paulk
    Jan 18, 2018 at 12:27
0

Since JMeter 3.3 you have __timeShift() function which can make your life easier. The relevant function call would be something like:

${__timeShift(MM/dd/YYYY HH:mm a,,P15D,,slotFirstCall)}

Demo:

JMeter Time Shift Example


Also be aware that starting from JMeter 3.1 it is recommended Groovy for all forms of scripting (general recommendation of avoiding scripting where possible still applies)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.