Java obtains the difference between two times.

Source: Internet
Author: User

I need to make a time difference judgment, and I don't want to write it. So I found out from the previous code that MI has... let's record it now.

Public static void main (String [] args) throws ParseException {

SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
Date currentTime = new Date ();
// Convert the intercepted time string into a Time Format String
Date beginTime = sdf. parse ("12:53:30 ");
// The default value is millisecond. Divide by 1000 to convert to seconds.
Long interval = (currentTime. getTime ()-beginTime. getTime ()/1000; // second
Long day = interval/(24*3600); // day
Long hour = interval % (24*3600)/3600; // hour
Long minute = interval % 3600/60; // minute
Long second = interval % 60; // second
System. out. println ("two time differences:" + day + "day" + hour + "hour" + minute + "minute" + second + "second ");

}

Author "xingcxb"
 

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.