0

Interface need set RFC1123 time in header I can't do it in beanshell using JMeter

I know code works in 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());

But I can't run it in Beanshell script

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 1
    Do you get any error? if so, can you add stacktrace in your question? – Milan Desai Jun 03 '19 at 13:05
  • 1
    I would also recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead in Java use the built-in `DateTimeFormatter.RFC_1123_DATE_TIME` and other classes from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 03 '19 at 13:08

2 Answers2

0

You must add import statement in start of your code:

import java.text.SimpleDateFormat;

Notice that you better move to using JSR223 components over Beanshell

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0

Header:

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

Code:

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

Output:

Mon, 3 Jun 2019 13:19:56 GMT

I am using java.time, the modern Java date and time API. SimpleDateFormat that you mentioned is notoriously troublesome and long outdated, you don’t want to use it.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    It's not jshell, you can check it online https://tio.run/#beanshell , see also [Beanshell Vs Jshell](https://github.com/beanshell/beanshell/issues/34) – Ori Marko Jun 03 '19 at 13:17
  • Code is still getting error `Class or variable not found: ZoneOffset.UTC` in beanshell, but it works with `JSR223 Sampler` – Ori Marko Jun 03 '19 at 13:29
  • Thanks for reporting, @user7294900. It’s interesting, I didn’t get that error. I also don’t when I follow the link at the bottom of my own answer. Wonder if your beanshell is based on an earlier Java version than the one you gave me? – Ole V.V. Jun 03 '19 at 13:29