java.util.Date somatório de datas  XML
Índice dos Fóruns » Java Básico
Autor Mensagem
JotaJota
Java Ninja
[Avatar]

Membro desde: 11/05/2007 03:36:11
Mensagens: 256
Localização: Araraquara
Offline

Olá pessoal
vejam se podem me ajudar

quero fazer o seguinte
eu tenho uma lista (List<Date> lista de horas no formato "HH:mm" essas horas são do tipo java.util.Date
eu preciso somar todas essas horas e colocar em uma outra propriedade (private Date somatorioHora do mesmo tipo:

Eu estou usando o Calendar aqui veja o metodo:


private Date somat(List<Acao> lista, Date horaTotal) {
Calendar calendario = getCalendar(horaTotal);
for (Acaoacao : lista) {
SimpleDateFormat conversor = new SimpleDateFormat("HH:mm");
conversor.format(acao.getTempoTrabalho);
Long l = conversor.getCalendar().getTimeInMillis();
calendario.add(Calendar.MILLISECOND, l.intValue());
}

return calendario.getTime();
}

private Calendar getCalendar(Date horaTotal) {
Calendar calendar = Calendar.getInstance();
if (esforco != null) {// se ele tiver com hora null ele nao atribui
calendar.setTime(horaTotal);
}
return calendar;
}


Não deu certo
eu selecionei
um item na lista...
e quando ele foi colocar "03:15" em horaTotal
a hora total ficou com "22:00"

se tem um na lista era apenas para colocar o valor 03:15 ....

então alguem sabe como resolver essa soma de horas?
vlw quem puder me ajudar
[Email]
CintiaDR
JavaEvangelist
[Avatar]

Membro desde: 01/10/2007 06:49:30
Mensagens: 476
Localização: Curitiba - PR
Offline

Na verdade nunca usei, mas li na documentação.

Vc poderia setar um Calendar pra cada data (como vc fez) e usar o "roll()" para somar a hora e minutos.




public void roll(int field,
int amount)

Adds a signed amount to the specified calendar field without changing larger fields. A negative roll amount means to subtract from field without changing larger fields. If the specified amount is 0, this method performs nothing.

This method calls Calendar.complete() before adding the amount so that all the calendar fields are normalized. If there is any calendar field having an out-of-range value in non-lenient mode, then an IllegalArgumentException is thrown.

Example: Consider a GregorianCalendar originally set to August 31, 1999. Calling roll(Calendar.MONTH, 8 ) sets the calendar to April 30, 1999. Using a GregorianCalendar, the DAY_OF_MONTH field cannot be 31 in the month April. DAY_OF_MONTH is set to the closest possible value, 30. The YEAR field maintains the value of 1999 because it is a larger field than MONTH.

Example: Consider a GregorianCalendar originally set to Sunday June 6, 1999. Calling roll(Calendar.WEEK_OF_MONTH, -1 ) sets the calendar to Tuesday June 1, 1999, whereas calling add(Calendar.WEEK_OF_MONTH, -1 ) sets the calendar to Sunday May 30, 1999. This is because the roll rule imposes an additional constraint: The MONTH must not change when the WEEK_OF_MONTH is rolled. Taken together with add rule 1, the resultant date must be between Tuesday June 1 and Saturday June 5. According to add rule 2, the DAY_OF_WEEK, an invariant when changing the WEEK_OF_MONTH, is set to Tuesday, the closest possible value to Sunday (where Sunday is the first day of the week).


http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html#roll(int,%20int)

This message was edited 2 times. Last update was at 03/06/2009 14:52:09


Faça Perguntas Inteligentes - NÃO me pergunte como

PelaMorDeQualquerCoisa, o GUJ não é penico! Google é seu amigo!
[MSN] [ICQ]
JotaJota
Java Ninja
[Avatar]

Membro desde: 11/05/2007 03:36:11
Mensagens: 256
Localização: Araraquara
Offline

vou testar
\o/
vlww
beijosmeliga =*
[Email]
 
Índice dos Fóruns » Java Básico
Ir para:   
Powered by JForum 2.1.8 © JForum Team