0

I have timestamp for one of my http sampler in following format

Tue Nov 07 10:28:10 PST 2017

and i need to convert it to in following format

11/07/2017 10:28:10

i tried different approaches but don't know what am i doing wrong.Can anyone help me on that.Thanks.

user7982813
  • 151
  • 1
  • 5
  • 20

2 Answers2

2

It's very similar to how you'd do it in Java.

Here's an example:

import java.text.DateFormat;
import java.text.SimpleDateFormat;

String string = "Tue Nov 07 10:28:10 PST 2017";

// Original format to convert from
DateFormat formatFrom = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

// Target format to convert to
DateFormat formatTo = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);

// Parse original string, using original format
Date date = formatFrom.parse(string);

// Convert to a target format
String result = formatTo.format(date);

// Just to show the output, not really necessary
log.info(result);

One catch: since target format omits the zone, local zone of the computer will be used. So for example original time 10:28:10 PST will be converted to 10:28:10 for computer in PST zone, but for computer in EST zone it will be converted to 13:28:10

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
1

I heard Groovy is the new black so given:

  1. Date class in Groovy SDK has format() and parse() methods
  2. It is recommended to use JSR223 Test Elements and Groovy language since JMeter 3.1

you can get the date converted in a single line of Groovy code like:

Date.parse("EEE MMM dd HH:mm:ss zzz yyyy", 'Tue Nov 07 10:28:10 PST 2017').format("dd/MM/yyyy HH:mm:ss", TimeZone.getTimeZone('PST'))

Demo:

JMeter Groovy Convert Date

Dmitri T
  • 159,985
  • 5
  • 83
  • 133