I have a method to calculate the expiry date given manufacturing date of format("yyyy-MM-dd") and the months before the product could be used in int.First I tried with getYear getMonth getDate of Month class as follows I dint got errors results:
public void calculateExpiryDate(List<Item> items)
{
Iterator<Item> itr=items.iterator();
while(itr.hasNext())
{
Item i=itr.next();
Date md=i.getManufacturingDate();
int ubm=i.getUseBeforeMonths();
Calendar c=new GregorianCalendar(md.getYear(),md.getMonth(),md.getDate());
//System.out.println(c);
c.add(Calendar.MONTH, ubm);
Date exp=c.getTime();
i.setExpiryDate(exp);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy MM dd");
System.out.println(md+" "+ubm+" "+sdf.format(exp)+" "+" "+i.getId());
}
}
But when I Quit using it and used setTime instead it solved my problem.I want to know what mistake I was making before and why things don't work that day and if any mistake(cause I was not getting any compile time errors) what is it actually.Following is the version of same code giving proper results.
public void calculateExpiryDate(List<Item> items)
{
Iterator<Item> itr=items.iterator();
while(itr.hasNext())
{
Item i=itr.next();
Date md=i.getManufacturingDate();
int ubm=i.getUseBeforeMonths();
Calendar c=new GregorianCalendar();
c.setTime(md);
//System.out.println(c);
c.add(Calendar.MONTH, ubm);
Date exp=c.getTime();
i.setExpiryDate(exp);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy MM dd");
System.out.println(md+" "+ubm+" "+sdf.format(exp)+" "+" "+i.getId());
}
}