While adding one day to the date in Java, not updating the month -


this question has answer here:

i hitting issue , want in 1 more.

below code block -

public date adddays(date date, int days)     {         calendar cal = calendar.getinstance();         cal.settime(date);         cal.add(calendar.date, days); //minus number decrement days         return cal.gettime();     }   startdate = "02/06/2016"; dateformat df = new simpledateformat("mm/dd/yyyy"); (int i=0; < 90; i++) {                 try{                     rawstartdate = df.parse(startdate);                     formattedstartdate = df.format(rawstartdate);                     string timebookedforperday = starthour + ":" + startmin + ":" + startslot + "-"                             + endhour + ":" + endmin + ":" + endslot;                     bookedfor.put(formattedstartdate, timebookedforperday);                     rawnextdate = adddays(rawstartdate, 1);                     startdate = df.format(rawnextdate);                 }                 catch ( exception ex ){                 }             } 

now issue hitting is, till time month february adding 1 day working fine iteratively. when month should change, starts iterating on same month.

also adding day date not taking care of number of days in month i.e. 29 in feb , 31 in march.

any pointers, can better way.

output (truncated bit) -

***start date***03/01/2016 ***start date***03/02/2016 ***start date***03/03/2016 ***start date***03/04/2016 ***start date***03/05/2016 ***start date***03/06/2016 ***start date***03/07/2016 ***start date***03/08/2016 ***start date***03/09/2016 ***start date***03/10/2016 ***start date***03/11/2016 ***start date***03/12/2016 ***start date***03/13/2016 ***start date***03/14/2016 ***start date***03/15/2016 ***start date***03/16/2016 ***start date***03/17/2016 ***start date***03/18/2016 ***start date***03/19/2016 ***start date***03/20/2016 ***start date***03/21/2016 ***start date***03/22/2016 ***start date***03/23/2016 ***start date***03/24/2016 ***start date***03/25/2016 ***start date***03/26/2016 ***start date***03/27/2016 ***start date***03/28/2016 ***start date***03/29/2016 ***start date***03/30/2016 ***start date***03/31/2016 ***start date***03/01/2016 ***start date***03/02/2016 ***start date***03/03/2016 ***start date***03/04/2016 ***start date***03/05/2016 ***start date***03/06/2016 

any suggestions or pointers or duplicate question can refer?

thanks,

aj

in simpledateformat mm minute mm month, date(month) updating dont see it, see minute, should edit format pattern like

dateformat df = new simpledateformat("mm/dd/yyyy"); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -