Java - Sumar periodos con Joda Time?

 
Vista:
sin imagen de perfil

Sumar periodos con Joda Time?

Publicado por Daniel Jesus (4 intervenciones) el 08/09/2014 20:37:23
MUY BUENAS PUES TENGO ESTE PROGRAMITA ECHO CON JODA DONDE PIDO AL USUARIO EL EPRIODO DE TIEMPO (DOS EFCHAS) Y ME DA COMO RESULTADO EL TIEMPO TRANSCURRIDO POR EJEMPLO USUARIO INGRESA FECHA1=10/10/2013 Y EN FECHA2=13/11/2014 DA COMO RESULTADO 1 AÑO 1 MES Y 3 DIAS.
AHORA AH ESO YO INGRESO OTRO PERIODO SI Y ME DEBE DE SUMAR LOS DOS PERIODOS COMO POR EJEMPLO INGRESO EL MISMO Y ME DEBE DE DAR LA SUMA QUE ES 2 AÑOS 2 MESES Y 6 DIAS. COMO PUEDO HACER PARA HACER ESE CONTEO DE FECHAS CADA QUE DESEA INGRESAR OTRO PERIODO???


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char opc;

do{
System.out.println("Ingresa primera fecha: ");
String fecha1 =sc.next();

System.out.println("Ingresa segunda fecha: ");
String fecha2 =sc.next();

Prueba3 obj = new Prueba3();
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd/M/yyyy");

try {
//dia-mes-año
Date date1 = simpleDateFormat.parse(fecha1);
Date date2 = simpleDateFormat.parse(fecha2);

obj.printDifference(date1, date2);

} catch (ParseException e) {
e.printStackTrace();
}

System.out.println("Desea ingresar otro periodo?: ");
opc=sc.next().charAt(0);
}while(opc == 's');
}

public void printDifference(Date startDate, Date endDate){

Interval interval =
new Interval(startDate.getTime(), endDate.getTime());
Period period = interval.toPeriod();
System.out.printf(
"%d years, %d months, %d days%n",
period.getYears(), period.getMonths(), period.getDays());
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder