android-calculate the number of days between two dates

Source: Internet
Author: User
Tags date1

To calculate the number of days between two dates, you must convert the two dates to a uniform timestamp, and then calculate their millisecond difference and change the millisecond difference to days.

System.out.print ("Start Time:"); String str1= BeginTime;//"YYYYMMDD" format such as 20131022System.out.println ("\ n End Time:"); String str2= _endtime;//"YYYYMMDD" format such as 20131022SimpleDateFormat SimpleDateFormat =NewSimpleDateFormat ("YyyyMMdd");//Enter a date formatDate date1 =NULL; Try{date1=Simpledateformat.parse (STR1); } Catch(ParseException e) {e.printstacktrace (); } Date date2=NULL; Try{date2=Simpledateformat.parse (STR2); } Catch(ParseException e) {e.printstacktrace (); } GregorianCalendar Cal1=NewGregorianCalendar (); GregorianCalendar Cal2=NewGregorianCalendar ();            Cal1.settime (date1);            Cal2.settime (DATE2); DoubleDaycount = (Cal2.gettimeinmillis ()-Cal1.gettimeinmillis ())/(1000 * 3600 * 24);//change from millisecond to interval number of daysSystem.out.println ("\ n Difference" + Daycount + "Day");

Excerpt from: http://www.2cto.com/kf/201310/252189.html

android-calculate the number of days between two dates

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.