How to do date arithmetics

technical benerator q&a

How to do date arithmetics

Postby hans.castorp » Wed Sep 18, 2013 8:59 am


Using Benerator 0.8.2

I'm trying to generate date values where one value must be bigger than the first value (those are two columns in the database from_date, until_date).

As those are pure Dates (no time), I am using a DayGenerator:

Code: Select all
<bean id="date_gen" class="org.databene.benerator.primitive.datetime.DayGenerator">
       <property name="min" value="2012-01-01"/>
       <property name="max" value="2020-01-01"/>
       <property name="granularity" value="0-0-1"/>
       <property name="distribution" value="{new RandomSequence()}"/>

Then my idea was to put the generated value into a variable and use scripting to add a random number of days to the start value. But my naive approach didn't work:
Code: Select all
<variable name="num_days" type="int" min="14" max="30"/>
<variable name="from_date" type="date" source="date_gen"/>
<variable name="until_date" type="date" script="{ben:from_date + num_days}"/>
<attribute name="VALID_FROM" type="date" source="from_date"/>
<attribute name="VALID_UNTIL" type="date" source="until_date"/>

This runs without an error, but valid_from and valid_until always have the same value. In Chapter "14.2.4 Date arithmetic" of the manual, the example shows: "new Date() + 5000" which made me assume that this is working. So is my syntax for the "script" wrong, or is this not working like that? num_days does contain random values, I verified that by including it in the output file.

How can I generate two dates like that?

Posts: 5
Joined: Tue Nov 01, 2011 1:04 pm

Return to Benerator Help

Who is online

Users browsing this forum: No registered users and 2 guests