0

我需要帮助,我有一个对象数组列表。该对象包含多个字段,我对此问题感兴趣的两个日期字段(date_panne date_mise and running)和两个其他时间字段(heure_panne and time start),并且我想获得两者之间的差异总和,(date_panne, heure_panne)(date_mise_en_marche; heure_mise_en_marche)给出失败的总时间。如果有人可以帮助我,我将不胜感激这是我的功能:

public String disponibile() throws Exception {

    int nbreArrets = 0;
    List<Intervention> allInterventions = interventionDAO.fetchAllIntervention();
    List<Intervention> listInterventions = new ArrayList<Intervention>();

    for (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" ;
}
4

1 回答 1

0

假设日期被截断为日期并且是 java.util.Date 类型,并且时间只包含小时、分钟、秒和毫秒并且也是 Date 类型,首先创建一个类似的方法

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

现在,只需循环遍历您想要求和的干预,将日期之间的差异计算为毫秒,然后将它们相加:

long totalMillis = 0L;
for (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;
}
于 2013-07-18T18:00:54.017 回答