I have a ContratMercan
with 2 Timestamps
containing start
and end
dates.
I have to create a new entity SeguimientoSeccion
for each month
between this 2 dates.
I have 2 questions:
- There is a way to improve it?
- After this I will need to manipulate the
List<SeguimientoSeccion> secciones
to see which one matches other entity... I though about making aMap
but there can be 2 or more sections in eachmonth
andyear
private List<SeguimientoSeccion> createSectionsPerMonth(ContratMercan contrato)
{
List<SeguimientoSeccion> secciones = new ArrayList<SeguimientoSeccion>();
Calendar start = Calendar.getInstance();
start.setTime(contrato.getPeriodoEntregaIni());
start.set(Calendar.DAY_OF_MONTH, 1);
Calendar end = Calendar.getInstance();
end.setTime(contrato.getPeriodoEntregaFin());
end.set(Calendar.DAY_OF_MONTH, 2); // for loop comparison
for (int month = start.get(Calendar.MONTH), year = start.get(Calendar.YEAR); start.before(end); month = start.get(Calendar.MONTH), year = start.get(Calendar.YEAR)) {
SeguimientoSeccion seccion = new SeguimientoSeccion();
seccion.setMonth(month + 1); // natural month number
seccion.setYear(year);
start.add(Calendar.MONTH, 1);
secciones.add(seccion);
}
return secciones;
}