jmeter convert date from EST to UTC

2019-04-11 15:53发布

问题:

I am working with dates that are on an API response. Date coming from API are in EST. I need to convert the EST (technically EDT) date to UTC and then compare with dates from another API response whose values are in UTC format

I am trying to do this with Javascript in JMeter. I have a "BSF PostProcessor" as a child of my "HTTP Request" sampler.

My input: endDate=2014-01-31T23:59:59

I tried a few options but none of them get me the value I am expecting. They all come back on my debug sampler as..

myNewDate=Invalid Date

dt1=Invalid Date

vars.put("myendDate", vars.get("endDate"));
var dt = new Date('myendDate');
vars.put("dt1", dt.toUTCString());

var myDate = new Date('${endDate}');
vars.put("myNewDate", myDate.toUTCString());


var myDate = new Date('${endDate}');
myDate.toISOString();
vars.put("myDate1", myDate);

I think my BSF PostProcessor with Javascript as Language is having trouble creating the Date object.

Any inputs please?

回答1:

Add groovy-all.jar to jmeter/lib folder

And use JSR223Sampler with Groovy and Compilation Cache key and following code:

import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.Date;

SimpleDateFormat estFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
SimpleDateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone estTime = TimeZone.getTimeZone("EST"); 
estFormat.setTimeZone(estTime);
gmtFormat.setTimeZone(gmtTime); 
String endDate = vars.get("endDate");
Date endDateAsDate= estFormat.parse(endDate);
vars.put("dt1", gmtFormat.format(endDateAsDate));

You can then use ${dt1}