Search code examples
jmeterjmeter-plugins

JMeter: Converting extracted time stamp value to date format


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


Solution

  • 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