Home > OS >  Add values between a date range
Add values between a date range

Time:07-17

Trying to write a program where I have some data of holdings in between date ranges in database. There might be two rows or three rows in between that date range. Each row has a holding value and I need to add each value in that date range. Can anyone suggest to me the solution? I tried this but it isn't working.

for(int i=0;i<filteredStartDate.size();i  ) {
            Long sum = null;
            if(filteredStartDate.get(i).isBefore((ChronoLocalDate) filteredEndDate)) {
                 sum = sum listHoldingsDBRecords.get(i).getHoldings();
                
            }
        }

Please help.

CodePudding user response:

I have recreated what I believe to be your situation. I am using a record for the Holdings.

public class SumDateRange {
    record Holdings(long getHoldings) {
    }
    
    public static void main(String[] args) {
        List<Holdings> listHoldingsDBRecords =
                List.of(new Holdings(10), new Holdings(20),
                        new Holdings(30), new Holdings(140),
                        new Holdings(150), new Holdings(160));
        
        List<LocalDate> filteredStartDate =
                List.of(LocalDate.parse("2022-01-15"),
                        LocalDate.parse("2022-01-20"),
                        LocalDate.parse("2022-01-30"),
                        LocalDate.parse("2022-02-15"),
                        LocalDate.parse("2022-02-15"),
                        LocalDate.parse("2022-02-15"));
        
        LocalDate filteredEndDate = LocalDate.parse("2022-01-31");
        
        long sum = 0;
        for (int i = 0; i < filteredStartDate.size(); i  ) {
            if (filteredStartDate.get(i).isBefore(filteredEndDate)) {
                sum = sum
                          listHoldingsDBRecords.get(i).getHoldings();
            }
        }
        System.out.println(sum);
    }
}

prints

60

Remember that any date before the end date is valid. So perhaps you need a minimum acceptable date and check for within a range:

if (filteredStartDate.get(i).isAfter(minimumAcceptableDate) && 
    filteredStartDate.get(i).isBefore(filteredEndDate)) {

But the above depends entirely on your ultimate goal and the data involved.

CodePudding user response:

As commented, generally best to that kind of work on the database. But your question is about Java, so here goes.

Regarding your use of ChronoLocalDate, if you study the Javadoc you’ll see that the abstract classes, interfaces, and superclasses are not intended for app development in java.time, generally. Those are for internal use by java.time itself, and for third parties implementing a chronology for the java.time framework.

To track a date range as a pair of dates, I suggest adding the ThreeTen-Extra library to your project. That library offers the LocalDateRange class to represent a pair of LocalDate objects. The class offers handy methods such as contains, abuts, etc.

To represent your data in Java, define a map with LocalDateRange as the key, and a List of your holding numbers. After map, you can loop on he entries to report a sum of each list.

Map< LocalDateRange , List< BigDecimal > > map = … ; 

This structure is known as a multimap, meaning each key maps to a collection of multiple values rather than to a single value.

All this has been covered many times on Stack Overflow. Search to learn more.

  •  Tags:  
  • java
  • Related