- # Get date one year in future
- The code below can be put in a BeanShell pre/postprocessor. It gets the current date, adds a year to it, and outputs the resulting variable into a JMeter one called ${oneyearinfuture} for use anywhere in the script. In the code, YEAR can be replaced by MINUTES or DAYS_IN_MONTH to add x minutes/days instead of years to the timestamp.
- ```
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
Date now = new Date(); // get current time
Calendar c = Calendar.getInstance(); // get Java Calendar instance
c.setTime(now); // set Calendar time to now
c.add(Calendar.YEAR, 1); // add 5 minutes to current time
Date now_plus_5_minutes = c.getTime(); // get Date value for amended time
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd"); // create a formatter for date
String mydate = sdf.format(now_plus_5_minutes); // format date as string
vars.put("oneyearinfuture",mydate); // save date to JMeter variable named "mydate"
//2015-07-11T23:59:59.000
```
- # URL encoder and decoder
- I used the BeanShell PostProcessor to take the extracted variable and decode it by putting in this code:
- `vars.put("viewstate",vars.get("viewstate").replace("%2F","/"));`
- This takes the variable `${viewstate}`, replaces all `%2F` with a slash, and saves it back into the variable for later use.
- # Save value into variable
- `vars.put(varName,varValue)`
- Example
- `vars.put("viewstate",vars.get("viewstate").replace("%2F","/"));`
- # Get value from a variable
- `vars.get(varName)`
- Example
- `vars.put("viewstate",vars.get("viewstate").replace("%2F","/"));`
-