JMeter:将提取的时间戳值转换为日期格式

美国时间,毫秒级的时间戳转换成时间格式:

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); 

亚洲时间,秒级别的转换成时间格式

import java.text.*; 
//long timeStamp = Long.parseLong(vars.get("time")); 
Date date = new Date(Long.parseLong(String.valueOf(${register_time}).concat("000"))); //用捕获的时间戳替换长值。转换成秒的形式,就是把字符串变成long类型的.java是静态语言,对类型有要求
DateFormat formatter = new SimpleDateFormat("MM-dd-YYYY HH:mm:ss"); 

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); 

 

posted @ 2020-04-21 12:53  tooltime  阅读(2758)  评论(0编辑  收藏  举报