【问题标题】:Get RFC1123_time in Beanshell ( using JMeter)在 Beanshell 中获取 RFC1123_time(使用 JMeter)
【发布时间】:2019-10-19 01:03:49
【问题描述】:

接口需要在 header 中设置 RFC1123 时间 我不能使用 JMeter 在 beanshell 中做到这一点

我知道代码在 java 中有效:

SimpleDateFormat sdf3 = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",Locale.US);
sdf3.setTimeZone(TimeZone.getTimeZone("GMT"));
String rfc1123_3 = sdf3.format(new Date());

但我无法在 Beanshell 脚本中运行它

【问题讨论】:

  • 你有什么错误吗?如果是这样,您可以在您的问题中添加堆栈跟踪吗?
  • 我也建议你不要使用SimpleDateFormat。这个类是出了名的麻烦而且已经过时了。而在 Java 中使用内置的 DateTimeFormatter.RFC_1123_DATE_TIME 和来自 java.time, the modern Java date and time API 的其他类。

标签: java jmeter beanshell jsr223 rfc1123


【解决方案1】:

您必须在代码开头添加 import 语句:

import java.text.SimpleDateFormat;

请注意,您最好转而使用 JSR223 组件而不是 Beanshell

【讨论】:

    【解决方案2】:

    标题:

    import java.time.*;
    import java.time.format.*;
    

    代码:

    print(OffsetDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME));
    

    输出:

    Mon, 3 Jun 2019 13:19:56 GMT
    

    我正在使用 java.time,现代 Java 日期和时间 API。你提到的SimpleDateFormat 出了名的麻烦而且过时了,你不想用它。

    链接

    【讨论】:

    • 不是jshell,可以在线查看tio.run/#beanshell,另见Beanshell Vs Jshell
    • 代码在 beanshell 中仍然出现错误 Class or variable not found: ZoneOffset.UTC,但它适用于 JSR223 Sampler
    • 感谢您的报告,@user7294900。有趣的是,我没有得到那个错误。当我点击我自己答案底部的链接时,我也不会这样做。想知道您的 beanshell 是否基于比您给我的版本更早的 Java 版本?
    猜你喜欢
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多