Calculate Difference Between Hours

0

I need to calculate the difference between hours, I am using xhtml and primefaces , I have the Time Starts and End Time , I want to add the difference in Total Hs . Follow my code:

<p:outputLabel value="Hora inicial:" />
<p:inputMask maxlength="5" size="5" value="#{horasBean.hora.horaini}" mask="99:99" required="true" requiredMessage="O campo 'Hora Inicial' é obrigatório" />

<p:outputLabel value="Hora Final:" />
<p:inputMask maxlength="5" size="5" mask="99:99" value="#{horasBean.hora.horafim}" required="true" requiredMessage="O campo 'Hora Final' é obrigatório" />

<p:outputLabel value="Total Hs:" />
<p:inputText maxlength="3" size="3" value="#{horasBean.hora.totalhoras}" />
    
asked by anonymous 18.01.2017 / 01:56

1 answer

0

As I understand it, you need to display the length of a period in String format. If so, try the following:

1) Insert getDescricaoDuracao() into XHTML:

<p:outputText value="#{horasBean.hora.getDescricaoDuracao()}" />

2) In the hora ed entity, insert the methods below:

(If you are using LocalDateTime )

public LocalDateTime getHoraInicial() {
    return horaInicial;
}

public LocalDateTime getHoraFinal() {
    return horaFinal;
}

public Duration calcularDuracao(LocalDateTime inicio, LocalDateTime fim) {
    return Duration.between(toInstant(inicio), toInstant(fim));
}

public Instant toInstant(LocalDateTime t) {
    return t.atZone(ZoneId.systemDefault()).toInstant();
}

(Or if you are using Date )

public Date getHoraInicial() {
    return horaInicial;
}

public Date getHoraFinal() {
    return horaFinal;
}

public Duration calcularDuracao(Date inicio, Date fim) {
    return Duration.between(toInstant(inicio), toInstant(fim));
}

public Instant toInstant(Date d) {
    return Instant.ofEpochMilli(d.getTime());
}

Common methods between LocalDateTime or Date :

public String getDescricaoDuracao() {
    return toString(calcularDuracao(getHoraInicial(), getHoraFinal()));
}

public String toString(Duration duracao) {
    String retorno = "";
    if (duracao != null) {
        String dia = "", hora = "", minuto = "";
        Long tDia = (long) 0, tHora = (long) 0, tMinuto = (long) 0;
        Map<TimeUnit,Long> i = dividirDuracao(duracao.toMillis());
        tDia = i.get(TimeUnit.DAYS);
        tHora = i.get(TimeUnit.HOURS);
        tMinuto = i.get(TimeUnit.MINUTES);
        dia = tDia + " d";
        hora = tHora + " h";
        minuto = tMinuto + " min";
        if (tDia > 0) {
            retorno = dia + " " + hora + " " + minuto;
        } else {
            if (tHora > 0) {
                retorno = hora + " " + minuto;
            } else {
                if (tMinuto > 0) {
                    retorno = minuto;
                } else {
                    retorno = "-";
                }
            }
        }
    } else {
        retorno = "-";
    }
    return retorno;
}

public Map<TimeUnit,Long> dividirDuracao(Long intervalo) {
    List<TimeUnit> units = new ArrayList<TimeUnit>(EnumSet.allOf(TimeUnit.class));
    Collections.reverse(units);
    Map<TimeUnit,Long> result = new LinkedHashMap<TimeUnit,Long>();
    long milliesRest = intervalo;
    for ( TimeUnit unit : units ) {
        if (milliesRest > 0) {
            long diff = unit.convert(milliesRest,TimeUnit.MILLISECONDS);
            long diffInMilliesForUnit = unit.toMillis(diff);
            milliesRest = milliesRest - diffInMilliesForUnit;
            result.put(unit,diff);
        } else {
            result.put(unit,(long) 0);
        }
    }
    return result;
}
    
19.01.2017 / 18:51