Ver Mensaje Individual
  #3 (permalink)  
Antiguo 26/11/2007, 19:23
Hats
 
Fecha de Ingreso: diciembre-2004
Mensajes: 418
Antigüedad: 19 años, 5 meses
Puntos: 2
Re: restar fechas en java

bueno, laverdad que aun no estoy del todo empapado en java, pero algo asi tb llegue a leer en otros foros, pero al final pude enocntrar la solucion:

con esto conviertes String a Date

public static Date aDate(String strFecha){
SimpleDateFormat formatoDelTexto = new SimpleDateFormat("yyyy/MM/dd");
Date fecha = null;

try {
fecha = formatoDelTexto.parse(strFecha);
} catch (java.text.ParseException ex) {
ex.printStackTrace();
}
return fecha;


}


y con esto las restas

public static int fechasDiferenciaEnDias(Date fechaInicial, Date fechaFinal) {

DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
String fechaInicioString = df.format(fechaInicial);
try {
fechaInicial = df.parse(fechaInicioString);
}
catch (ParseException ex) {
}

String fechaFinalString = df.format(fechaFinal);
try {
fechaFinal = df.parse(fechaFinalString);
}
catch (ParseException ex) {
}

long fechaInicialMs = fechaInicial.getTime();
long fechaFinalMs = fechaFinal.getTime();
long diferencia = fechaFinalMs - fechaInicialMs;
double dias = Math.floor(diferencia / (1000 * 60 * 60 * 24));
return ( (int) dias);
}

gracias de todas fromas por tu ayuda , hasta otra.