date - How to get the minutes from two times in java -


i trying difference between 2 hours following

string time1 = "05:00 am"; string time2 = "06:20 am";  string format = "hh:mm aa";  simpledateformat sdf = new simpledateformat ( format );  date dateobj1 = null; date dateobj2 = null; try {     dateobj1 = sdf.parse ( time1 );     dateobj2 = sdf.parse ( time2 ); } catch ( parseexception e ) {     e.printstacktrace ( ); }  long diff = dateobj2.gettime ( ) - dateobj1.gettime ( ); double diffinhours = diff / ( ( double ) 1000 * 60 * 60 ); double diffinmins = ( diffinhours + ( int ) diffinhours ) * 60; 

dump console.

system.out.println ( "diff: " + diff ); system.out.println ( "diffinhours: " + diffinhours ); system.out.println ( "diffinmins " + diffinmins ); 

when run.

diffinhours 1.3333333333333333 minutes 139.99999999999997 

i calculate time in minutes between 2 times in above case should 80 minutes giving other value.

would 1 me minutes between 2 time?

with java 8 this:

datetimeformatter fmt = datetimeformatter.ofpattern("hh:mm a"); localtime t1 = localtime.parse(time1, fmt); localtime t2 = localtime.parse(time2, fmt); long minutes = chronounit.minutes.between(t1, t2); //80 

note not take dst changes account.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -