jmeter之beanshell使用:常用变量汇总

文章介绍了在Java中使用变量存储日期,包括如何获取当前日期、前一天日期的方法,并在beanshell预处理上下文中应用这些技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.变量--日期

使用场景:当入参日期是变量,取当前日期

使用如下:

(1)当前日期

import java.text.SimpleDateFormat;
import java.util.Date;
 
// 创建 SimpleDateFormat 对象并指定日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
// 获取当前时间
Date currentDate = new Date();
 
// 将当前时间按照指定格式转换为字符串
String formattedDate = dateFormat.format(currentDate);
 
// 输出结果
log.info("当前日期:" + formattedDate);
vars.put("currentDate",formattedDate);

添加: beanshell预处理

接口请求入参:引用 currentDate

请求运行后:

今天时间为2023-12-15日,查看取值正确

(2)当前日期的前一天

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
 
// 创建 SimpleDateFormat 对象并指定日期格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
// 获取当前时间
Date currentDate = new Date();
 
// 将当前时间按照指定格式转换为字符串
String formattedDate = dateFormat.format(currentDate);


// 使用 Calendar 类来获取前一天的日期
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(Calendar.DATE, -1);
Date yesterdayDate = calendar.getTime();

// 将前一天的日期按照指定格式转换为字符串
String lastoneDate = dateFormat.format(yesterdayDate);

 
// 输出结果
log.info("当前日期:" + formattedDate);
vars.put("currentDate",formattedDate);
vars.put("lastoneDate",lastoneDate);

请求参数里引用 ${}

调用后:

---其他待后续使用到再补充,定期汇总

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海是倒过来的天~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值