10000 - Updated implementation of the getMonthsBetween() method · javaxt-project/javaxt-core@717dabe · GitHub
[go: up one dir, main page]

Skip to content

Commit 717dabe

Browse files
committed
- Updated implementation of the getMonthsBetween() method
git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1593 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent cf68538 commit 717dabe

File tree

1 file changed

+34
-58
lines changed

1 file changed

+34
-58
lines changed

src/javaxt/utils/Date.java

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -759,91 +759,67 @@ private static double getMonthsBetween(LocalDate start, LocalDate end) {
759759

760760
//If we're still here, compute fractions
761761
double fraction = 0.0;
762-
if (m==0){
762+
if (m==0 && start.getMonthValue()==end.getMonthValue()){
763763

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();
778766

779767
}
780768
else{
781769

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));
783775

776+
if (start.getDayOfMonth()>maxDays){
784777

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);
795780

781+
//Calulate months between the start date and the new date
782+
m = ChronoUnit.MONTHS.between(start, d);
796783

797-
//Compute fractions
798-
double numDays = 0.0;
799-
while (!d.equals(end)){
784+
//Calculate fraction
785+
if (startIsLastDayInMonth && endIsLastDayInMonth){}
786+
else{
800787

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));
804793
}
8000 805-
806-
d = d.plusDays(1);
807-
numDays++;
808794
}
809-
fraction += numDays/(double)d.lengthOfMonth();
810-
811795
}
812796
else{
813797

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-
828798
//Calulate months between the start date and the new end date
829799
m = ChronoUnit.MONTHS.between(start, e2);
830800

831-
801+
//Calculate fraction
832802
if (e2.isAfter(end)){
833803

834804
//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+
}
837814

838815
}
839-
else{
816+
else if (e2.isBefore(end)){
840817

841818
//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+
844821
}
845822
}
846-
847823
}
848824

849825

0 commit comments

Comments
 (0)
0