spring - failed to manipulate my Arraylist -
i need , have arraylist of objects . object contains multiple fields i'm interested in question 2 date fields (date_panne date_mise , running) , 2 other time fields (heure_panne , time start), , obtain sum of difference between (date_panne, heure_panne) , (date_mise_en_marche; heure_mise_en_marche) give total time of failure. if can me please gratful function :
public string disponibile() throws exception { int nbrearrets = 0; list<intervention> allinterventions = interventiondao.fetchallintervention(); list<intervention> listinterventions = new arraylist<intervention>(); (intervention currentintervention : allinterventions) { if (currentintervention.getid_machine() == this.intervention.getid_machine() && currentintervention.getdate_panne().compareto(getproductionstartdate()) >= 0 && currentintervention.getdate_panne().compareto(getproductionenddate()) <= 0) { listinterventions.add(currentintervention); } } savedinterventionlist = listinterventions; return "successview" ; }
assuming the dates truncated day , of type java.util.date, , times contain hours, minutes, seconds , milliseconds , of type date, start creating method like
private date combine(date dateonly, date timeonly) { calendar datecalendar = calendar.getinstance(); datecalendar.settime(dateonly); calendar timecalendar = calendar.getinstance(); timecalendar.settime(timeonly); datecalendar.add(calendar.hour_of_day, timecalendar.get(calendar.hour_of_day)); datecalendar.add(calendar.minute, timecalendar.get(calendar.minute)); datecalendar.add(calendar.second, timecalendar.get(calendar.second)); datecalendar.add(calendar.millisecond, timecalendar.get(calendar.millisecond)); return datecalendar.gettime(); } now, it's matter of looping through interventions want sum, computing difference between dates milliseconds, , add them:
long totalmillis = 0l; (intervention intervention : interventions) { date marche = combine(intervention.getdatemiseenmarche(), intervention.gettimemiseenmarche()); date panne = combine(intervention.getdatepanne(), intervention.gettimepanne()); long differenceinmillis = marche.gettime() - panne.gettime(); totalmillis += differenceinmillis; }
Comments
Post a Comment