Calculates the difference in the number of days between two dates C++/java

Source: Internet
Author: User
Tags date1

1--java

Parsing: Calling the Calendar class in Java

intnew  calendar.getinstance ();  Cal.settime (date1);   int time1 = cal.get (calendar.day_of_year);  Cal.settime (DATE2);   int time2 = cal.get (calendar.day_of_year);  //long days = Math.Abs (time1-time2)/(24*3600*1000); return Math.Abs (day1-day2);}  

2--java

int  Days (Date date1,date date2) {  long time1 = Math.Abs (Date1.gettime ()-date2.gettime ());  return (int) time1/(24*3600*1000);}

Analysis: First of all to know about the concept of leap year, every four years a run, 400 years run, and can not be divisible by 100. Because, leap year February has 29 days, a total of 366 days.

Pseudocode: First the date string of the year, month, day separate, type YYYY-MM-DD, get two dates, year1,month1,day1,year2,month2,day2

If the year, the month is the same:

return day1-day2; (large decrease)

If the year is the same, the months are different:

Calculate date1, how many days in the year d1;date2, how many days of the year D2

return d1-d2;

If the year is different:

D1=date1 is the number of days of the year

D2=date2 How many days before the end of the year

d3= the number of days between the two year difference

Return d1+d2+d

#include <iostream>using namespace std; PublicBOOL Stringtodate (string date,int&year,int&month,int&Day ) { Year= Atoi (Date.substr (0,4). C_STR ()); Month=atio (Date.substr (5,2). C_STR ()); Day=atio (Date.substr (8,2). C_STR ()); day[12]={31,29,31,30,31,30,31,31,30,31,30,31}; if(Islear (year)) day[1]=29; returnYear>0&&month>0&&month<=12&&day<=day[month-1];} PublicBOOL Isleap (intYear ) {return((year%4==0) | | (year%400==0)) && (year&100!=0);} Public intDaysinyear (intYearintMonth,intDay ) {intDays =Day ; intDay[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if(Isleap (year)) day[1]=29;  for(inti=0;i<month-1;i++) { days+=Day[i]; }returnDays ;} Publicbetween_days (String date1,string date2) {if(Stringtodate (Date1,int&AMP;YEAR1,int&month1,int&day1) &&stringtodate (String date2,int&AMP;YEAR2,int&month2,int&day2)) {if(year1==year2&&month1==month2) {returnday1>day2?day1-day2:day2-Day1; }Else if(year1==year2) {intD1 =daysinyear (year1,month1,day1); intD2 =daysinyear (year2,month2,day2); returnd1>d2?d1-d2:d2-D1; }Else{if(year1<year2)      {swap (YEAR1,YEAR2);      Swap (MONTH1,MONTH2);    Swap (DAY1,DAY2); }intD1=0;  for(intj=year2+1;j<year1;j++) {if(Isleap (j)) D1+=366; ElseD1+=365; }intD2=daysinyear (year1,month1,day1); if(Isleap (year2)) D2=d2+366-daysinyear (year2,month2,day2); ElseD2=d2+365-daysinyear (year2,month2,day2); returnd1+D2; }}}

      

    

      

Calculates the difference in the number of days between two dates C++/java

Related Article

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.