Text Resizer Text Resizer
July 30th, 2010
You're browsing: Scriptmatico.Com » JAVA » Calculando la edad en JAVA

Calculando la edad en JAVA

Posted on May 09 in JAVAby adminPrintText Resizer Text Resizer

Este sencillo codigo que encontre en una web para calcular la edad de una persona.

JAVA:
  1. public int calcularEdad(Calendar fechaNac){
  2.     Calendar today = Calendar.getInstance();
  3.  
  4.     int diff_year = today.get(Calendar.YEAR) - fechaNac.get(Calendar.YEAR);
  5.     int diff_month = today.get(Calendar.MONTH) - fechaNac.get(Calendar.MONTH);
  6.     int diff_day = today.get(Calendar.DAY_OF_MONTH) - fechaNac.get(Calendar.DAY_OF_MONTH);
  7.  
  8.     //Si está en ese año pero todavía no los ha cumplido
  9.     if(diff_month<0 || (diff_month==0 && diff_day<0)){
  10.         diff_year = diff_year - 1; //no aparecían los dos guiones del postincremento :|
  11.     }
  12.     return diff_year;
  13. }

Post to Twitter


  • No Related Post

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Back to Top
[x] Cerrar
E-mail