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

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

请求参数里引用 ${}

调用后:

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

相关推荐

  1. jmeterbeanshell使用

    2024-01-02 17:56:01       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-02 17:56:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-02 17:56:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-02 17:56:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-02 17:56:01       18 阅读

热门阅读

  1. C++递归/递归函数(详细讲解)

    2024-01-02 17:56:01       39 阅读
  2. C++拷贝构造函数介绍

    2024-01-02 17:56:01       34 阅读
  3. hive多分隔符外表支持

    2024-01-02 17:56:01       33 阅读
  4. vue解决执行打包之后打开页面空白问题

    2024-01-02 17:56:01       38 阅读
  5. 如何用GPT制作技术路线图?

    2024-01-02 17:56:01       35 阅读
  6. MySQL 8.1 和 8.2 中 EXPLAIN 的新玩法

    2024-01-02 17:56:01       37 阅读
  7. 机房使用超融合系统的卓越优势

    2024-01-02 17:56:01       52 阅读
  8. 机器学习一些概念

    2024-01-02 17:56:01       35 阅读
  9. c语言-数据类型详细介绍

    2024-01-02 17:56:01       41 阅读
  10. CentOS7安装docker

    2024-01-02 17:56:01       40 阅读