2

Possible Duplicate:
How can I find the number of days betwen two Dates?

I am facing a problem on how to get the number of days that have been elapsed by comparing 2 dates. Currently i try to use compareTo() but it only returns either 1 or-1, I want to get the number of days that have passed. Thannks

Community
  • 1
  • 1

4 Answers4

3

This should work its java code. Just modify to work with android

import java.util.*;
 public class DateDifference {
 public static void main(String args[]){
 DateDifference difference = new DateDifference();
 }
DateDifference() {
Calendar cal1 = new GregorianCalendar();
Calendar cal2 = new GregorianCalendar();

cal1.set(2008, 8, 1); 
cal2.set(2008, 9, 31);
System.out.println("Days= "+daysBetween(cal1.getTime(),cal2.getTime()));
}
public int daysBetween(Date d1, Date d2){
return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
}
}
mpeerman
  • 2,050
  • 2
  • 16
  • 16
1

The compareTo() method (if you use Data/DateTime() as an example) will only give you that :

Return an integer that indicates whether this instance is earlier than, the same as, or later than the specified Date/DateTime value. (MSDN)

It is why you only get numbers between -1 and 1 and not the number of days (you can check on MSDN befor

In order to do what you want that, check this post : Number of days between 2 dates

Community
  • 1
  • 1
ChapMic
  • 26,954
  • 1
  • 21
  • 20
1

I hope this will help you

 import java.util.*; import java.util.Calendar;

 public class DeltaDays { public static void main(String[] args) {
// Creates two calendars instances 

Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance();

 // Set the date for both of the calendar instance 
calendar1.set(2007, 12, 30); calendar2.set(2008, 05, 03);

 // Get the represented date in milliseconds
 long milis1 = calendar1.getTimeInMillis(); long milis2 = calendar2.getTimeInMillis();

// Calculate difference in milliseconds 
long day = milis2 - milis1;

 // Calculate difference in seconds 
long diffSeconds = day / 1000;

 // Calculate difference in minutes 
long diffMinutes = day / (60 * 1000);

 // Calculate difference in hours 
long diffHours = day / (60 * 60 * 1000);

 // Calculate difference in days 
long diffDays = day / (24 * 60 * 60 * 1000);

 System.out.println("In milliseconds: " + day + " milliseconds.");
 System.out.println("In seconds: " + diffSeconds + " seconds.");
 System.out.println("In minutes: " + diffMinutes + " minutes.");
 System.out.println("In hours: " + diffHours + " hours.");
 System.out.println("In days: " + diffDays + " days."); } }
Ghost Answer
  • 1,458
  • 1
  • 17
  • 41
0

Try this,Use dateDifference() method on your android project:

import java.util.Calendar;
import java.util.Date;

public class Sample {

public static void main(String arg[])
{

dateDifference();

}

public static void dateDifference()
{ 
Date currentdate = new Date();
Calendar calUpdated = Calendar.getInstance();
calUpdated.set(2012, 01, 01);
long currentDateMilliSec = currentdate.getTime();
long updateDateMilliSec = calUpdated.getTimeInMillis();
long diffDays = (currentDateMilliSec - updateDateMilliSec) / (24 * 60 * 60 * 1000);
System.out.println("Time in days: " + diffDays + " days.");
}
}
Ali Hasan
  • 1,045
  • 4
  • 16
  • 43