Jmeter Randomization Part 2 – Random Date

Here is a java code example that will generate a random date between 1900 and 2000:

import java.text.SimpleDateFormat;

// randomize DOB
int rndMonth = ${__Random(1,12)};
int rndDay = ${__Random(1,28)};
int rndYear = ${__Random(1900,2000)};
String rndDob = rndMonth + "/" + rndDay + "/" + rndYear;
SimpleDateFormat source = new SimpleDateFormat("mm/dd/yyyy");
SimpleDateFormat target = new SimpleDateFormat("yyyy-mm-dd");
Date date = source.parse(rndDob);
// convert to yyyy-mm-dd format
String RNDDOB = target.format(date);
log.info("Date is = " + RNDDOB);

This is what you should see in the log console (with different random values for every thread):

02_logConsole

Leave a comment