The next example shows how to create and use a collection of mock dates. A
collection, named dates, is created with a range whose start
value is 01/02/2003 04:05:06. Its step is 0/1/0
0:1:0, which causes each value to increment by 1 month and 1 minute.
System.out.println ("Dates2");
Dates dates = new Dates ("[01/02/2003 04:05:06]0/1/0 0:1:0");
DatamixerIterator iter = dates.getDatamixerIterator ();
iter.setFormatPattern ("yyyy/MM/dd HH:mm:ss");
// run
try
{
for (int i = 0; i < 5; i++)
{
Value value = iter.getNextValue ();
System.out.println (value.toString ());
}
}
catch (DatamixerException e)
{
System.err.println (e.getMessage ());
}
An iterator named iter is created against the collection. Its
format pattern is set, with the position of day and year fields reversed
from where they are in the range. Next, 5 values are printed. Note that
getNextValue(), which returns a Value, is called
on the iterator. A Value overrides
java.lang.toString() to print its value, applying formatting if
available.
The result is:
2003/02/01 04:05:06
2003/03/01 04:06:06
2003/04/01 04:07:06
2003/05/01 04:08:06
2003/06/01 04:09:06
Equivalent code, using XML configuration, is:
<dm:dates range="[01/02/2003 04:05:06]0/1/0 0:0:0">
<dm:iterator id="iter0" format="yyyy/MM/dd HH:mm:ss"/>
</dm:dates>
The Java code to load and run this XML looks like the code in the next
example. The exact code is in the file
examples/java/Config.java, in inner class
DatesRunner.
DatamixerContext context = root.getContext ();
DatamixerIterator iter = context.getDatamixerIterator ("iter0");
for (int i = 0; i < 5; i++)
System.out.println (iter.getNextValue ());