简体   繁体   中英

Retrieve data from list by the most recent date java

I'm trying to collect data from my list (or group) with the most recent date for example the last monday. So I could then add values together then insert the new values back into the list.

Currently my output is looking like this

ID   Date       charge    balance
1   29/07/2013    10        100
2   29/07/2013    20        200
3   29/07/2013    30        300

Give or take a few columns. The main purpose is to group for example the entries that are the 29th, add the charge to the balance create a new entry / field / row with the new balance and the system date.

So far I've create my list and I'm able to read in the entries from csv using a scanner but i have no idea how i can get the code to get the dates without hard coding.

public class Statements {

    public int tncy_num;
    //public double net_rent;
    public String end_date;
    public double bal_val;
    public double chg_val;

    public Statements(int t, String ed, double bv ,  double cv){
        tncy_num = t;
        //net_rent = nr;
        end_date = ed;
        bal_val = bv;
        chg_val= cv;
    }

    public int getTncynum(){return tncy_num;}
    //public double getNetRentnum(){return net_rent;}
    public String getEndDatenum(){return end_date;}
    public double getBalValnum(){return bal_val;}
    public double getChgValnum(){return chg_val;}
    //public double getBenfValnum(){return benf_val;}
    //public double getAdjVal(){return adj_val;}
    //public double getTotlValnum(){return totl_val;}

    public String toString(){       
        return " "+this.getTncynum()+
                " "+this.getEndDatenum()+
                " "+this.getBalValnum()+
                " "+this.getChgValnum();
    }

}

I have a main driver class which has the main method(used to simply run the script), a coordinator class used to get the data and set the location of the csv file and other classes which return the data of the list.

public class Calculations {
private Coordinator cord;
private ArrayList Data;

public Calculations(Coordinator co) {
    cord =co;
    Data = new ArrayList<Statements>(cord.getData());
    System.out.print(Data); 
}
}

Here an example :

 Collections.sort(myList, new Comparator<Statements>() {
  public int compare(Statements o1, Statements o2) {
    if (o1.getEndDate() == null || o2.getEndDate() == null)
       return 0;
    return o1.getEndDate().compareTo(o2.getEndDate());
 }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM