Java gets two milliseconds of a date difference

Source: Internet
Author: User

Method one can use the GetTime () method of date to convert the time of the current date format to the number of milliseconds and subtract from it.

Long systime = new Date (). GetTime ();//Current system time
Long oldtime = Old.gettime ();//Comparison of time
Long time = (systime-oldtime);//Millisecond of difference

Method two uses the calendar's Gettimeinmillis () method to convert the time of the current date format to the number of milliseconds.


Calendar nowdate=calendar.getinstance ();
Calendar olddate=calendar.getinstance ();
Nowdate.settime (New Date ());//set to current system time
Olddate.settime (old);//set to the date you want to compare
Long Timenow=nowdate.gettimeinmillis ();
Long Timeold=olddate.gettimeinmillis ();
Long time = (timenow-timeold);//Millisecond of difference

The number of milliseconds to get a two-day difference can get two dates apart in a few days when a few seconds.

Java gets two milliseconds of a date difference

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.