android - Why my date comparison not working? -


i have compare user entered date current date & time. using below code.

datetimestring = ondate.concat(" ").concat(ontime);                     simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm");                      try {                         dateobject = simpledateformat.parse(datetimestring);                         if (new date().after(dateobject)) {                             dateobject.setyear(new date().getyear());                              datetimestring = simpledateformat.format(dateobject);                             dateobject = simpledateformat.parse(datetimestring);                             if (new date().after(dateobject)) {                                 dateobject.setyear(new date().getyear() + 1);                                  dateobjectstring = simpledateformat.format(dateobject);                             }                             else {                                 dateobjectstring = simpledateformat.format(dateobject);                             }                         }                         else {                             dateobjectstring = simpledateformat.format(dateobject);                         }                     }                     catch (parseexception e) {                         e.printstacktrace();                     } 

the if blocks in above code executing every time value of datetimestring. want them execute when entered date before current date. please notify doing wrong. have tried other methods doesn't got success. when parsing string date date object converts hour of day 00.

try this,

public static date getremainderdate(string ondate, string ontime) throws parseexception {          string datetimestring = ondate.concat(" ").concat(ontime);         simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm");          try {             date dateobject = simpledateformat.parse(datetimestring);             date curdate=new date();             if (curdate.after(dateobject)) {                 dateobject.setyear(curdate.getyear());                  if(curdate.after(dateobject)){                     dateobject.setyear(curdate.getyear()+1);                     return dateobject;                 }else{                     return dateobject;                 }             }             else {                 return dateobject;             }         }         catch (parseexception e) {             e.printstacktrace();         }         return null;     } 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -