JMeter: Converting extracted time stamp value to date format - jmeter

I have to extract the timestamp value from a response and have to pass it as a parameter to next request.
I have extracted the timestamp value from Regular Expression Extractor.
Time stamp value is 1481086800000
Value to be passed is in the format(Month/Date/Year HH:mm)- 12/07/2016 10:30
Kindly provide your valuable suggestion on how to convert the extracted time stamp value into above date format.

Following code directly converted epoch timestamp to AKST timezone. No need of two samplers as suggested in the comments.
Add JSR223 Sampler, select Groovy and add the following code:
import java.text.*;
//long timeStamp = Long.parseLong(vars.get("time"));
Date date = new Date(1481086800000); //replace the long value with timeStamp you captured.
DateFormat formatter = new SimpleDateFormat("MM/dd/YYYY HH:mm");
TimeZone tzInAmerica = TimeZone.getTimeZone("America/Anchorage");
formatter.setTimeZone(tzInAmerica);
String dateFormatted = formatter.format(date);
vars.put("newDate", dateFormatted); //access new value using ${newDate}, in your script.
log.info(dateFormatted);
Screenshot reference:

Related

Double time specification in file in laravel api

I am making api's in laravel and getting 2021-01-30T10:30:17.704 05:30 from $request->followup and i have column in database named followup having datetime datatype. But it's giving me following error.
Carbon\Exceptions\InvalidFormatException: Could not parse '2021-01-30T10:30:17.704 05:30': DateTime::__construct(): Failed to parse time string (2021-01-30T10:30:17.704 05:30) at position 24 (0): Double time specification in file D:\xampp2\htdocs\synocrm-baid\rest-apis\vendor\nesbot\carbon\src\Carbon\Traits\Creator.php on line 188
I tried to change format like
$followupDate = date('Y-m-d h:i:s A',strtotime($request->followup));
My modal having
#property Carbon|null $followup
$request->followup is appending datetime with datetime
Original Value: 2021-01-30T10:30:17.704 05:30
Datetime: 2021-01-30T10:30:17.704
Time: 05:30
As we can see, there is two Time (05:30 and 10:30:17), because of which the strtotime() cannot convert the value.
In order to fix the problem,
Send the value as time time only (2021-01-30T10:30:17.704) instead of two times Time attribute
If you have no control over request value, you can retrieve only date value like:
$dateValue = explode(' ', $request->followup)[0];
$followupDate = date('Y-m-d h:i:s A',strtotime($dateValue));
Or Using Carbon
$followupDate = Carbon::parse($dateValue);
I would not recommend option 2 because it creates problems and confusion for other devs.
2021-01-30T10:30:17.704 05:30
It does not seem to valid timestamp. For parsing, it should be the valid timestamp or date.

jmeter how to add +4 to the date extracted from the jmeter response using regular expression extractor?

I am facing issue while trying to add 4 days to the date extracted from the response.
I have extracted the date using regular expression extractor and placed in a variable depdate . Now i need one more date with +4 of extracted date .
Add a Beanshell PostProcessor after the Regular Expression Extractor
Put the following code into the PostProcessor's "Script" area:
import java.text.SimpleDateFormat;
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy"); // change it according to your Date format
Date originalDate = sdf.parse(vars.get("depdate"));
Calendar cal = Calendar.getInstance();
cal.setTime(originalDate);
cal.add(Calendar.DAY_OF_YEAR, 4); // change it if you need to add something else
Date newDate = cal.getTime();
vars.put("newDepdate", sdf.format(newDate));
log.info("Original date: " + vars.get("depdate"));
log.info("New date: " + vars.get("newDepdate"));
Refer the new date as ${newDapdate} where required
Remarks:
Above code assumes date in dd/mm/yyyy format, i.e. 23/08/2016. If your date format is different - use your own pattern, check out Customizing Formats article for details.
Above code assumes adding 4 days to the current date. If you need to add 4 minutes, hours, months, years, whatever - change Calendar.DAY_OF_YEAR to your value, see Calendar class JavaDoc for available options
For anything else check out How to Use BeanShell: JMeter's Favorite Built-in Component guide

Calculating Age by custom Program

There is one POST request which submits both Date of Birth & Age.
I found the Date of Birth from previous request, extracted it through Regular Expression Extractor, and passing the Variable in the POST request.
But I did not found the Age from the previous request. Tried figuring out the Age element through Firebug also. But it's not located, due to this field is auto calculated by the Date of Birth field and also the field is read-only.
How do I get the value of Age field?
Possible solution could be using some Jmeter in-built function or by writing some Program in any Scripting Language. But I do not know how to do it in Jmeter.
Thanks in advance.
Given that you have variable i.e. "birthdate" with the value of "1970-05-15" you can figure out person's age with the Beanshell PreProcessor as follows:
Add a Beanshell PreProcessor as a child of the sampler which needs to pass birth date and age.
Put the following code into the PreProcessor's "Script" area
import java.text.SimpleDateFormat;
String birthDate = vars.get("birthDate");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
Date dateOfBirth = sdf.parse(birthDate);
Calendar dob = Calendar.getInstance();
dob.setTime(dateOfBirth);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.MONTH) < dob.get(Calendar.MONTH)) {
age--;
} else if (today.get(Calendar.MONTH) == dob.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) < dob.get(Calendar.DAY_OF_MONTH)) {
age--;
}
vars.put("age",String.valueOf(age));
You will be able to refer calculated age as ${age}
See SimpleDateFormat JavaDoc for explanation of "yyyy-mm-dd" pattern and information on how to define the one which matches your application date and time format and How to use BeanShell: JMeter's favorite built-in component guide to learn about scripting in JMeter.
Without seeing the response I can't say whether is can be extracted or not. A viable workaround would be to add a Post Processor to the request that the DOB is extracted from (this can be a jsr223 or beanshell) from there you can grab the DOB as such:
String dob = vars.get{"dateOfBirth"}
From there you will need to parse this into a date and compare it with the current date to get the age. You then need to put the "Age" variable so JMeter can access it.
vars.put("age", String.valueOf(age))

How to getHourOfDay from a timestamp using java.time?

From a java.util.Date( a timestamp), how can I get the hour of day?
In joda.time I use getHourOfDay().
There are multiple solutions for this. If you wish to use the Java 8 classes from java.time the following you need to covert a Date to one of the DateTime classes. The following can be used to convert a Date to a ZonedDateTime where you then can get the hour:
Date date = new Date();
// Convert to java 8 ZonedDateTime
Date date = new Date();
final ZonedDateTime dateTime = date.toInstant()
.atZone(ZoneId.systemDefault());
// Get the hour
int hour = dateTime.getHour();
Quite verbose as you have noticed but the simple reason for this is that a Date is sort of an Instant
Despite its name, java.util.Date represents an instant on the time-line, not a "date". The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC).
Another approach is simply to get the field from a Calendar instance.
final Calendar instance = Calendar.getInstance();
instance.setTime(date);
final int hourOfDay = instance.get(Calendar.HOUR_OF_DAY);

Concatenate DateTimePicker in VS2010

How can I concatenate the values of two datetimepicker?
DateTimePicker1 - this is for the the DATE mm/dd/yyyy
DateTimePicker2 - this is for the the TIME hh:mm:ss
I want a value of ... mm/dd/yyy hh:mm:ss in one variable, how can I do that?
One easy way would be
DateTime dt = new DateTime(dtp1.Year, dtp1.Month, dtp1.Day, dtp2.Hour, dtp2.Minute, dtp2.Second);
Please check the order of the parameters, I am writing from memory.
Here dtp1 is a DateTime object from the first datetime picker.

Resources