3

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.

First JSR223 Sampler

First JSR223 Sampler

Debug Sampler

Vishal
  • 147
  • 4
  • 16

1 Answers1

4

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:

enter image description here

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Thank you Naveen for the help, as the time stamp is now converted to date,could you please provide suggestion on how to convert the same date(i.e 12/07/2016 10:30) into another timezone(i.e AKST Alaska Standard Time ) , as the date is displayed in GMT. – Vishal Jan 05 '17 at 09:44
  • 1
    you can try the answer here http://stackoverflow.com/a/18026349/2575259. using TimeZone class. – Naveen Kumar R B Jan 05 '17 at 09:46
  • Hi Naveen, As per your suggestion , i have used both the codes in my script. For the first JSR223 Sampler, i have given input as "1481086800000". The date is successfully converted to 12/07/2016 10:30:00 In second JSR223 Sampler, output from first script is taken as input. The date is converted to AKST format as "12/27/2016 10:30:00". The application requires date as 12/06/2016 20:00(AKST timezone),for which i am unable to convert. Kindly provide your suggestions for the same. Please refer the attached screen shot. – Vishal Jan 19 '17 at 06:24
  • 1
    1. After converting in first JSR223 sampler, `12/07/2016 10:30:00` is in `IST` format, but not `GMT`. so, specify `IST` in place of `GMT`. – Naveen Kumar R B Jan 19 '17 at 07:07
  • Thank you for the suggestion Naveen, i tried for IST in place of GMT, but the date displayed is 12/27/2016 05:00 – Vishal Jan 19 '17 at 07:21
  • updated the answer. no need of two samplers. use only one sampler and keep the code and run. it is giving me `12/06/2016 20:00` – Naveen Kumar R B Jan 19 '17 at 07:47
  • you can accept my answer if it worked for you :). please consider accepting (tick mark) the answer for all your questions (if it really helped you). This is how you help other community members to find the right answer and grateful to the guy who answered it. – Naveen Kumar R B Jan 19 '17 at 08:18