Prezados bom dia,
estou utilizando a api jodatime para calcular diferenças entre datas nada muito complicado, ate estudei ela pelos post no forum, o problema e o seguinte:
percebe se o codigo que comentei usei as classes diretas Years, Months, Days etc… para pegar os tempos decorridos de um determinado periodo… e dessa manera que preciso total de horas minutos segundos dentre duas datas , e se ja passou anos, meses e dias ate ai tudo bem… so que ta talculando errado ai tentei usar uma clase Period do jodatime mas tambem da erro
obs: Periodo e uma classe bean com getters e setters nada de mais;
segue o codigo:
[code]
public static Periodo getPeriodo(String dateI, String timeI, String dateF, String timeF) {
Periodo periodo = new Periodo();
int diaI = 0;
int mesI = 0;
int anoI = 0;
int horaI = 0;
int minutoI = 0;
int segundoI = 0;
int diaF = 0;
int mesF = 0;
int anoF = 0;
int horaF = 0;
int minutoF = 0;
int segundoF = 0;
String vdataI[] = dateI.split("-");
String vdataF[] = dateF.split("-");
diaI = new Integer(vdataI[2]);
mesI = new Integer(vdataI[1]);
anoI = new Integer(vdataI[0]);
diaF = new Integer(vdataF[2]);
mesF = new Integer(vdataF[1]);
anoF = new Integer(vdataF[0]);
String dhoraI[] = timeI.split("[:]");
horaI = new Integer(dhoraI[0]);
minutoI = new Integer(dhoraI[1]);
segundoI = new Integer(dhoraI[2]);
String dhoraF[] = timeI.split("[:]");
horaI = new Integer(dhoraF[0]);
minutoI = new Integer(dhoraF[1]);
segundoI = new Integer(dhoraF[2]);
//DateTime inicio = new DateTime(anoI, mesI, diaI, horaI, minutoI, segundoI, 0);
//DateTime fim = new DateTime(anoF, mesF, diaF, horaF, minutoF, segundoF, 0);
DateTime inicio = new DateTime(anoI, mesI, diaI, horaI, minutoI, segundoI, 0);
DateTime fim = new DateTime(anoF, mesF, diaI, horaF, minutoF, segundoF, 0);
Period period = new Period(inicio, fim);
/*System.out.println("Years:" + Years.yearsBetween(inicio, fim).getYears());
System.out.println("Months: " + Months.monthsBetween(inicio, fim).getMonths());
System.out.println("Days: " + Days.daysBetween(inicio, fim).getDays());
System.out.println("Hours: " + Hours.hoursBetween(inicio, fim).getHours());
System.out.println("Minutes: " + Minutes.minutesBetween(inicio, fim).getMinutes());
System.out.println("Seconds: " + Seconds.secondsBetween(inicio, fim).getSeconds());*/
System.out.println(anoI + " m" + mesI + " d" + diaI + " h" + horaI + " m" + minutoI + " s" + segundoI);
System.out.println(dateI + " " + timeI);
System.out.println(dateF + " " + timeF);
int dia = 0;
int mes = 0;
int ano = 0;
int hora = 0;
int minuto = 0;
int segundo = 0;
/*dia = Days.daysBetween(inicio, fim).getDays();
mes = Months.monthsBetween(inicio, fim).getMonths();
ano = Years.yearsBetween(inicio, fim).getYears();
hora = Hours.hoursBetween(inicio, fim).getHours();
minuto = Minutes.minutesBetween(inicio, fim).getMinutes();
segundo = Seconds.secondsBetween(inicio, fim).getSeconds();*/
dia = period.getDays();
mes = period.getMonths();
ano = period.getYears();
hora = period.getHours();
minuto = period.getMinutes();
segundo = period.getSeconds();
periodo.setDia(dia);
periodo.setMes(mes);
periodo.setAno(ano);
periodo.setHora(hora);
periodo.setMinuto(minuto);
periodo.setSegundo(segundo);
return periodo;
}[/code]
se eu inicializo assim:
DateTime inicio = new DateTime(2012, 5, 14, 18, 16, 40,0);
DateTime fim = new DateTime(2012, 5, 15, 9, 31, 52, 0);
System.out.println("Years:"+Years.yearsBetween(inicio, fim).getYears());
System.out.println("Months: "+Months.monthsBetween(inicio, fim).getMonths());
System.out.println("Days: "+Days.daysBetween(inicio, fim).getDays());
System.out.println("Hours: "+Hours.hoursBetween(inicio, fim).getHours());
System.out.println("Minutes: "+Minutes.minutesBetween(inicio, fim).getMinutes());
System.out.println("Seconds: "+Seconds.secondsBetween(inicio, fim).getSeconds());
Years:0
Months: 0
Days: 0
Hours: 15
Minutes: 915
Seconds: 54912
funciona, mas quando criei meu metodo citado aiem cima ele calcula errado nao sei porque seria a forma de inicializar? ele pede inteiros na hora de inicializar o DateTime(2012, 5, 14, 18, 16, 40,0);
DateTime inicio = new DateTime(2012, 5, 14, 18, 16, 40,0);
DateTime fim = new DateTime(2012, 5, 15, 9, 31, 52, 0);
se inicializo com minhas variáveis ele me mostra os resutados errados ja debuguei e estou passando as datas e horas corretissimas…
DateTime inicio = new DateTime(anoI, mesI, diaI, horaI, minutoI, segundoI, 0);
DateTime fim = new DateTime(anoF, mesF, diaI, horaF, minutoF, segundoF, 0)
serultado:
234923 Ano: 0 Mes: 0 Dia: 0 <-----> Horas: -18 : Minutos: -16 : Segundos: -40 DataCadastro: 2012-05-14 18:16:40 datatime atual: 15/05/2012 09:31:52
2012 m5 d14 h18 m16 s40
2012-05-14 18:16:40
2012-05-15 09:31:52
alguem me ajude !
no aguardo: