Java computing time difference

Source: Internet
Author: User

/**
* Calculate the time difference between two dates
* @ Param formattime1
* @ Param formattime2
* @ Return
*/
Public static string gettimedifference (timestamp formattime1, timestamp formattime2 ){
Simpledateformat timeformat = new simpledateformat ("yyyy-mm-dd, HH: mm: SS ");
Long T1 = 0l ;
Long T2 = 0l ;
Try {
T1 = timeformat. parse (gettimestampnumberformat (formattime1). gettime ();
} Catch (parseexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Try {
T2 = timeformat. parse (gettimestampnumberformat (formattime2). gettime ();
} Catch (parseexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
// Because the t1-t2 gets the millisecond level, so the first 3600000 gets the hour. The same is true for days or seconds.
Int hours = (INT) (T1-T2)/3600000 );
Int minutes = (INT) (T1-T2)/1000-hours * 3600)/60 );
Int second = (INT) (T1-T2)/1000-hours * 3600-minutes * 60 );
Return "" + hours + "Hour" + minutes + "Minute" + second + "second ";
}
/**
* Formatting time
* Locale is used to set language-sensitive operations.
* @ Param formattime
* @ Return
*/
Public static string gettimestampnumberformat (timestamp formattime ){
Simpledateformat m_format = new simpledateformat ("yyyy-mm-dd, HH: mm: SS", new locale ("ZH", "cn "));
Return m_format.format (formattime );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.