Time

2 respostas
Deluxe

pessoal…

alguem sabe como faz pra somar times ?

eu tenho dois horários em String

10:00:00
como eu faço pra somar com outro?

10:00:00 + 11:00:00 por exemplo

2 Respostas

thiago.correa

Bem tosco mas para mostrar o funcionamento:

Date d1 = new Date();
Date d2 = new Date();
System.out.println(new Date(d2.getTime() + d1.getTime()));
M

Se a intencao for trabalhar com Strings

public class Tempos2 {

	public static void main(String args[]) {

		String hora1 = "01:10:55";
		String hora2 = "01:55:10";

		String horas = "00";
		String minutos = "00";
		String segundos = "00";

		int sub = 0;
		int subHoras = 0;
		int subMinutos = 0;

		int segundos1 = (Integer.parseInt(hora1.substring(0, 2)) * 3600) + (Integer.parseInt(hora1.substring(3, 5)) * 60) + Integer.parseInt(hora1.substring(6, 8));
		int segundos2 = (Integer.parseInt(hora2.substring(0, 2)) * 3600) + (Integer.parseInt(hora2.substring(3, 5)) * 60) + Integer.parseInt(hora2.substring(6, 8));

		
		sub = segundos2 + segundos1;	

		if (sub >= 3600) {
			subHoras = (sub - (sub % 3600)) / 3600;
			sub = sub - (subHoras * 3600);
			if (subHoras < 10) {
				horas = "0" + Integer.toString(subHoras);
			} else {
				horas = Integer.toString(subHoras);
			}
		}

		if (sub >= 60) {
			subMinutos = (sub - (sub % 60)) / 60;
			sub = sub - (subMinutos * 60);
			if (subMinutos < 10) {
				minutos = "0" + Integer.toString(subMinutos);
			} else {
				minutos = Integer.toString(subMinutos);
			}
		}

		if (sub < 10) {
			segundos = "0" + Integer.toString(sub);
		} else {
			segundos = Integer.toString(sub);
		}

		System.out.println("HH:MM:SS : " + horas + ":" + minutos + ":" + segundos);

	}
}
Criado 11 de novembro de 2008
Ultima resposta 11 de nov. de 2008
Respostas 2
Participantes 3