Restar dos fechas en JAVA

util para restar una fecha de la actual del sistema, pero aqui se puede inferir para restar dos fechas cualesquiera:

 

import java.util.*;
class Prueba
{
 
 public static int getDaysCount(int year, int month, int day)
 {
  // year – the year minus 1900.
      // month – the month between 0-11.
      // date – the day of the month between 1-31
  Date now = new Date(System.currentTimeMillis());  
  Date date= new Date(year,month,day);
  
  if(now.before(date)) return -1;
  
  long diffmillis = now.getTime() – date.getTime();
  
  int ndays = (int)((((diffmillis / 1000) / 60) / 60) / 24);
  
  return ndays;
 }
 
 
 public static void main(String args[])
 {
  int n = Prueba.getDaysCount(105,0,20);
  
  if(n==-1) System.out.println("fecha no existente\n");
  
  else System.out.println("Numero de dias pasados: "+n+"\n");
  
 }
}

Leave a comment

No comments yet.

Comments RSS TrackBack Identifier URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

  • QR Code: po5i

    qrcode
  • Twitter Updates

  • RSS Youtube favorites

    • An error has occurred; the feed is probably down. Try again later.
  • RSS last.fm (recent)

    • An error has occurred; the feed is probably down. Try again later.
  • Visitantes

  • Bitácora

  • del.icio.us