android - Java Date from long returning incorrect day -


i writing android app, , uses datepicker. date returned in form year, monthofyear, dayofmonth.

from there can format date so:

            simpledateformat format = new simpledateformat("yyyy-mm-dd");             try {                 date d = format.parse(year + "-" + (monthofyear + 1) + "-" + dayofmonth);                 string dstring = d.tostring().substring(0, 10) + " " +  year;                 if(startend) {                     startdatetext.settext(dstring);                     startdate = new date(year, monthofyear, dayofmonth);                     log.d("start date set- ", startdate.tostring());                 } else {                     enddatetext.settext(dstring);                     enddate = new date(year, monthofyear, dayofmonth);                     log.d("end date set- ", enddate.tostring());                 }              } catch (parseexception e) {                 log.d("exception- ", e.getmessage());             } 

the if(startend) part referencing 2 different textviews, range of dates being collected.

at point data goes wrong. values returned correct. example when select saturday, january 2nd values 2016 1 2, year month day.

the value of dstring correct, ends sat jan 02 2016.

however, when call startdate.tostring(), value returned sun jan 02 00:00:00 gmt+00:00 3916 day off.

this happens on every date, , don't know why.

the long values of startdate , enddate passed through setresult() used elsewhere. dates produced these long values produce same 1 day off date.

thanks in advance help

if check out date javadoc you'll see constructor you're calling requires year-1900. 2016 provide 116 year:

new date( 116, 0, 2 ); 

further, month needs specified 0 indexed. january 0 not 1.

i should note it's deprecated constructor. should prefer calendar.set(year + 1900, month, date) or gregoriancalendar(year + 1900, month, date).


Comments