viernes, 8 de junio de 2012

Calcular años

 private static void calcularAnnos(){
      Date fechaNacimiento=new Date(87, 05, 8);
      Date fechaHoy =new Date();
      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
      System.out.println("Fecha nacimiento:"+dateFormat.format(fechaNacimiento));
      System.out.println("Fecha nacimiento:"+dateFormat.format(fechaHoy));
      int edad=fechaHoy.getYear()-fechaNacimiento.getYear();
      System.out.println("Edad:"+edad);
      System.out.println(fechaNacimiento.getMonth()+" "+fechaHoy.getMonth());
      if((fechaNacimiento.getMonth()-fechaHoy.getMonth())>0){
        edad--;
      }
 else if((fechaNacimiento.getMonth()-fechaHoy.getMonth())==0){
     System.out.println(fechaNacimiento.getDate()+" "+fechaHoy.getDate());
       if((fechaNacimiento.getDate()-fechaHoy.getDate())>0){
       edad--;
       }
 }

       System.out.println("Edad:"+edad);
    }

No hay comentarios:

Publicar un comentario