@@ -759,91 +759,67 @@ private static double getMonthsBetween(LocalDate start, LocalDate end) {
759
759
760
760
//If we're still here, compute fractions
761
761
double fraction = 0.0 ;
762
- if (m ==0 ){
762
+ if (m ==0 && start . getMonthValue ()== end . getMonthValue () ){
763
763
764
- //Simply add up the days between the dates
765
- double numDays = 0.0 ;
766
- LocalDate d = LocalDate .of (start .getYear (), start .getMonthValue (), start .getDayOfMonth ());
767
- while (!d .equals (end )){
768
-
769
- if (d .getDayOfMonth () == d .lengthOfMonth ()){
770
- fraction += numDays /(double )d .lengthOfMonth ();
771
- numDays = 0.0 ;
772
- }
773
-
774
- d = d .plusDays (1 );
775
- numDays ++;
776
- }
777
- fraction += numDays /(double )d .lengthOfMonth ();
764
+ //Simply compare the days of the month
765
+ fraction = (end .getDayOfMonth ()-start .getDayOfMonth ())/(double )end .lengthOfMonth ();
778
766
779
767
}
780
768
else {
781
769
782
- if (start .getDayOfMonth ()>end .lengthOfMonth () || endIsLastDayInMonth ){
770
+ //Create new end date using the original end date. Adjust the day
771
+ //of the month to match the start date. The new date will be either
772
+ //before or after the original end date.
773
+ int maxDays = LocalDate .of (end .getYear (), end .getMonthValue (), 1 ).lengthOfMonth ();
774
+ LocalDate e2 = LocalDate .of (end .getYear (), end .getMonthValue (), Math .min (start .getDayOfMonth (), maxDays ));
783
775
776
+ if (start .getDayOfMonth ()>maxDays ){
784
777
785
- LocalDate d = LocalDate .of (start .getYear (), start .getMonthValue (), start .getDayOfMonth ());
786
-
787
-
788
- //Wind back start date (d) a month or two before the end date
789
- m --;
790
- d = addOrSubtractMonths (m , d );
791
- while (d .isAfter (end )){
792
- d = addOrSubtractMonths (-1 , d );
793
- m --;
794
- }
778
+ //Create new date a few days after the end of the month
779
+ LocalDate d = e2 .plusDays (start .getDayOfMonth ()-maxDays );
795
780
781
+ //Calulate months between the start date and the new date
782
+ m = ChronoUnit .MONTHS .between (start , d );
796
783
797
- //Compute fractions
798
- double numDays = 0.0 ;
799
- while (! d . equals ( end )) {
784
+ //Calculate fraction
785
+ if ( startIsLastDayInMonth && endIsLastDayInMonth ){}
786
+ else {
800
787
801
- if (d .getDayOfMonth () == d .lengthOfMonth ()){
802
- fraction += numDays /(double )d .lengthOfMonth ();
803
- numDays = 0.0 ;
788
+ if (!startIsLastDayInMonth ){
789
+ fraction = -((start .lengthOfMonth ()-start .getDayOfMonth ())/(double )start .lengthOfMonth ());
790
+ }
791
+ else {
792
+ fraction = -(1 -((end .getDayOfMonth ())/(double )maxDays ));
804
793
}
8000
805
-
806
- d = d .plusDays (1 );
807
- numDays ++;
808
794
}
809
- fraction += numDays /(double )d .lengthOfMonth ();
810
-
811
795
}
812
796
else {
813
797
814
-
815
- //Create new end date using the original end date. Adjust the day
816
- //of the month to match the start date. The new date will be
817
- //either before or after the original end date.
818
- LocalDate e2 ;
819
- try {
820
- e2 = LocalDate .of (end .getYear (), end .getMonthValue (), start .getDayOfMonth ());
821
- }
822
- catch (Exception e ){ //leap year exception
823
- e2 = LocalDate .of (end .getYear (), end .getMonthValue (), start .getDayOfMonth ()-1 );
824
- e .printStackTrace ();
825
- }
826
-
827
-
828
798
//Calulate months between the start date and the new end date
829
799
m = ChronoUnit .MONTHS .between (start , e2 );
830
800
831
-
801
+ //Calculate fraction
832
802
if (e2 .isAfter (end )){
833
803
834
804
//subtract from e2
835
- double f = (e2 .getDayOfMonth ()-end .getDayOfMonth ())/(double )end .lengthOfMonth ();
836
- fraction -= f ;
805
+ int n = e2 .getDayOfMonth ()-end .getDayOfMonth ();
806
+ double f = (double )n /(double )end .lengthOfMonth ();
807
+
808
+ if (m ==0 ){
809
+ fraction = 1 -f ;
810
+ }
811
+ else {
812
+ fraction = -f ;
813
+ }
837
814
838
815
}
839
- else {
816
+ else if ( e2 . isBefore ( end )) {
840
817
841
818
//add from e2
842
- double f = (end .getDayOfMonth ()-e2 .getDayOfMonth ())/(double )end .lengthOfMonth ();
843
- fraction += f ;
819
+ fraction = (end .getDayOfMonth ()-start .getDayOfMonth ())/(double )end .lengthOfMonth ();
820
+
844
821
}
845
822
}
846
-
847
823
}
848
824
849
825
0 commit comments