Search code examples
jmeterbeanshell

How to convert datetime in jmeter using beanshell sampler


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.


Solution

  • 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