fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. System.out.println(diasDifferencia("02/01/2002","01/04/2002"));
  13. }
  14.  
  15. public static double diasDifferencia(String fechaInit, String fechaEnd){
  16.  
  17. String[] dateInit = fechaInit.split("/");
  18. String[] dateEnd = fechaEnd.split("/");
  19.  
  20. Date dInit = new GregorianCalendar(Integer.parseInt(dateInit[2]), Integer.parseInt(dateInit[1])-1, Integer.parseInt(dateInit[0])).getTime();
  21.  
  22. Date dEnd = new GregorianCalendar(Integer.parseInt(dateEnd[2]), Integer.parseInt(dateEnd[1])-1, Integer.parseInt(dateEnd[0])).getTime();
  23.  
  24. GregorianCalendar dEndLocale = new GregorianCalendar(new Locale("es", "ES"));
  25. GregorianCalendar dInitLocale = new GregorianCalendar(new Locale("es", "ES"));
  26.  
  27. dEndLocale.set(Integer.parseInt(dateEnd[2]), Integer.parseInt(dateEnd[1])-1, Integer.parseInt(dateEnd[0]));
  28. dInitLocale.set(Integer.parseInt(dateInit[2]), Integer.parseInt(dateInit[1])-1, Integer.parseInt(dateInit[0]));
  29.  
  30. System.out.println((dEndLocale.getTime().getTime() - dInitLocale.getTime().getTime())/(1000 * 60 * 60 * 24.0));
  31. System.out.println((dEndLocale.getTime().getTime() - dInit.getTime())/(1000 * 60 * 60 * 24.0));
  32. double diff = (dEnd.getTime() - dInit.getTime())/(1000 * 60 * 60 * 24.0);
  33.  
  34. return diff;
  35.  
  36. }
  37. }
Success #stdin #stdout 0.12s 321024KB
stdin
Standard input is empty
stdout
89.0
89.6273585300926
89.0