In my application, DOB field having limit of specific interval. I want to capture random dates between 1-Jan-1950 to 31-Dec-2000. please suggest. thanks in advance for your valuable suggestions.
Asked
Active
Viewed 3,321 times
2 Answers
0
You can use bean shell script sampler to call java apis and using Joda time - java library you can generate the dates between two specific dates as shown below( Ref: Java Generate all dates between x and y) and pick list item dates in random wise, Refer this link for using Beanshell in jmeter.
import java.util.ArrayList;
import java.util.List;
import org.joda.time.DateTime;
public class DateQuestion {
public static List<DateTime> getDateRange(DateTime start, DateTime end) {
List<DateTime> ret = new ArrayList<DateTime>();
DateTime tmp = start;
while(tmp.isBefore(end) || tmp.equals(end)) {
ret.add(tmp);
tmp = tmp.plusDays(1);
}
return ret;
}
public static void main(String[] args) {
DateTime start = DateTime.parse("2012-1-1");
System.out.println("Start: " + start);
DateTime end = DateTime.parse("2012-12-31");
System.out.println("End: " + end);
List<DateTime> between = getDateRange(start, end);
for (DateTime d : between) {
System.out.println(" " + d);
}
}
}
-
let me know how to use it in jmeter, please tell me some steps – Chetan Jun 24 '14 at 10:57
0
You can use Beanshell preprocessor to get a Random DOB. Just copy & paste the below code. It should work.
I am storing the random date in the 'dob' variable. So, just use ${dob} in the date of birth field.
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
dateFormat1 = new SimpleDateFormat("d-M-yyyy");
dateFormat2 = new SimpleDateFormat("dd-MMM-yyyy");
int randBetween(int start, int end) {
return start + (int)Math.round(Math.random() * (end - start));
}
gc = new GregorianCalendar();
year = randBetween(1950, 2000);
gc.set(gc.YEAR, year);
dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR));
gc.set(gc.DAY_OF_YEAR, dayOfYear);
vars.put("dob", (dateFormat2.format(dateFormat1.parse(gc.get(gc.DAY_OF_MONTH) + "-" + (gc.get(Calendar.MONTH)+1) + "-" + gc.get(Calendar.YEAR)))));

vins
- 15,030
- 3
- 36
- 47