Not able to get exact difference in number of days between two days using Java -
i'm working on getting number of days between 2 dates using java. when give application date 27 shows 0, , when give 26 shows 0. there wrong in code? ideas appreciated.
here code
public static void main (string[] args) throws java.lang.exception { dateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); date currentdate = new date(); system.out.println("current date:"+dateformat.format(currentdate)); simpledateformat formatter = new simpledateformat("yyyy-mm-dd hh:mm:ss"); string strappdate = "2014-10-28 15:11:30.0"; date appdate = formatter.parse(strappdate); system.out.println("application date :"+formatter.format(appdate)); long diff = appdate.gettime() - currentdate.gettime(); long diffdays = diff / (24 * 60 * 60 * 1000); system.out.println("diff days :"+diffdays); }
you can seek in way. utilize timeunit.milliseconds.todays()
long duration = appdate.gettime() - currentdate.gettime(); long diffinseconds = timeunit.milliseconds.toseconds(duration); long diffinminutes = timeunit.milliseconds.tominutes(duration); long diffinhours = timeunit.milliseconds.tohours(duration); long diffindays = timeunit.milliseconds.todays(duration); // number of days
java
No comments:
Post a Comment