java - Calendar functionality -


input date 2016-01-01, why output shows 2016/02/01?

string df = "2016-01-01"; string enddate=""; simpledateformat date_format_query = new simpledateformat("yyyymmdd't'hhmmss'z'");  calendar cal=calendar.getinstance(); string[] datestr=df.split("-");  int year=integer.parseint(datestr[0]); int month=integer.parseint(datestr[1]); int day=integer.parseint(datestr[2]); cal.set(year,month,day,23, 59,59); system.out.println(cal.gettime()); enddate=date_format_query.format(cal.gettime()); system.out.println(enddate); 

output:

mon feb 01 23:59:59 est 2016 20160201t235959z 

answer question

input date 2016-01-01, why output shows 2016/02/01?

because calendar::month 0-based.

month - value used set month calendar field. month value 0-based. e.g., 0 january.

you should use

int month=integer.parseint(datestr[1] - 1); 

correct solution

never parse manually string containing date, better date simpledateformat , use set calendar time:

simpledateformat dfo = new simpledateformat("yyyy-mm-dd"); calendar cal=calendar.getinstance(); cal.settime(dfo.parse("2016-01-01")); 

output:

fri jan 01 00:00:00 cet 2016 20160101t000000z 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -