jmeter convert date from EST to UTC

旧街凉风 提交于 2019-12-06 08:37:06

问题


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}



来源:https://stackoverflow.com/questions/18007111/jmeter-convert-date-from-est-to-utc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!