Jmeter获取当前日期函数

本文介绍如何使用JMeter的时间函数和Beanshell脚本获取当前日期,并演示如何进行日期加减操作及将日期赋值给变量。

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

目录

一、time函数

二、beanshell脚本

JMeter获取当前日期加1天:

JMeter获取当前日期前一天

JMeter获取当前日期并赋值给变量


一、time函数

time函数的使用方法:${__time(日期时间格式,自定义变量名)} 

年:yyyy年,MM 月,dd日,时:HH(24小时制)  hh(12小时制)   分:mm   秒:ss    毫秒:SSS

${__time(yyyy-MM-dd)}
${__time(dd-MM-yyyy)}
${__time(yyyyMMdd)}
${__time(dd/MM/yyyy)}

二、beanshell脚本

使用beanshell生成当前日期。

import java.util.Date;
import java.text.SimpleDateFormat;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String currentDate = dateFormat.format(new Date());
vars.put("currentDate", currentDate);

上述代码定义了一个SimpleDateFormat对象,用于将日期格式化为yyyy-MM-dd的格式,然后使用该对象将当前日期格式化为字符串。最后将该字符串存储到JMeter变量currentDate中。

JMeter获取当前日期加1天:

import java.util.Calendar;
import java.text.SimpleDateFormat;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, 1);
String currentDatePlus1 = dateFormat.format(calendar.getTime());
vars.put("currentDatePlus1", currentDatePlus1);

JMeter获取当前日期前一天

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_YEAR, -1);
String currentDateMinus1 = dateFormat.format(calendar.getTime());
vars.put("currentDateMinus1", currentDateMinus1);

JMeter获取当前日期并赋值给变量

import java.text.SimpleDateFormat;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String variableName = "currentDate";
String currentDate = dateFormat.format(new Date());
vars.put(variableName, currentDate);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值