找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 5222|回复: 0

BeanShell常用变量

[复制链接]

204

主题

64

回帖

876

积分

管理员

积分
876
发表于 2024-7-4 17:16:07 | 显示全部楼层 |阅读模式
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);


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Android开发工具,脚本 ( 粤ICP备2024275843号 )

GMT+8, 2026-4-16 02:53 , Processed in 0.022531 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表