I am getting the current date (in format 12/31/1999 i.e. mm/dd/yyyy) as using the below code:
Textview txtViewData;
txtViewDate.setText(\"Today is \" +
Date userDob = new SimpleDateFormat("yyyy-MM-dd").parse(dob);
Date today = new Date();
long diff = today.getTime() - userDob.getTime();
int numOfDays = (int) (diff / (1000 * 60 * 60 * 24));
int hours = (int) (diff / (1000 * 60 * 60));
int minutes = (int) (diff / (1000 * 60));
int seconds = (int) (diff / (1000));
This fragment accounts for daylight savings time and is O(1).
private final static long MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;
private static long getDateToLong(Date date) {
return Date.UTC(date.getYear(), date.getMonth(), date.getDate(), 0, 0, 0);
}
public static int getSignedDiffInDays(Date beginDate, Date endDate) {
long beginMS = getDateToLong(beginDate);
long endMS = getDateToLong(endDate);
long diff = (endMS - beginMS) / (MILLISECS_PER_DAY);
return (int)diff;
}
public static int getUnsignedDiffInDays(Date beginDate, Date endDate) {
return Math.abs(getSignedDiffInDays(beginDate, endDate));
}
Use the following functions:
/**
* Returns the number of days between two dates. The time part of the
* days is ignored in this calculation, so 2007-01-01 13:00 and 2007-01-02 05:00
* have one day inbetween.
*/
public static long daysBetween(Date firstDate, Date secondDate) {
// We only use the date part of the given dates
long firstSeconds = truncateToDate(firstDate).getTime()/1000;
long secondSeconds = truncateToDate(secondDate).getTime()/1000;
// Just taking the difference of the millis.
// These will not be exactly multiples of 24*60*60, since there
// might be daylight saving time somewhere inbetween. However, we can
// say that by adding a half day and rounding down afterwards, we always
// get the full days.
long difference = secondSeconds-firstSeconds;
// Adding half a day
if( difference >= 0 ) {
difference += SECONDS_PER_DAY/2; // plus half a day in seconds
} else {
difference -= SECONDS_PER_DAY/2; // minus half a day in seconds
}
// Rounding down to days
difference /= SECONDS_PER_DAY;
return difference;
}
/**
* Truncates a date to the date part alone.
*/
@SuppressWarnings("deprecation")
public static Date truncateToDate(Date d) {
if( d instanceof java.sql.Date ) {
return d; // java.sql.Date is already truncated to date. And raises an
// Exception if we try to set hours, minutes or seconds.
}
d = (Date)d.clone();
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setTime(((d.getTime()/1000)*1000));
return d;
}
Most of the answers were good and right for your problem of
so i want to find the difference between date in number of days, how do i find difference in days?
I suggest this very simple and straightforward approach that is guaranteed to give you the correct difference in any time zone:
int difference=
((int)((startDate.getTime()/(24*60*60*1000))
-(int)(endDate.getTime()/(24*60*60*1000))));
And that's it!
This is Simple and best calculation for me and may be for you.
try {
/// String CurrDate= "10/6/2013";
/// String PrvvDate= "10/7/2013";
Date date1 = null;
Date date2 = null;
SimpleDateFormat df = new SimpleDateFormat("M/dd/yyyy");
date1 = df.parse(CurrDate);
date2 = df.parse(PrvvDate);
long diff = Math.abs(date1.getTime() - date2.getTime());
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println(diffDays);
} catch (Exception e1) {
System.out.println("exception " + e1);
}
I found a very easy way to do this and it's what I'm using in my app.
Let's say you have the dates in Time objects (or whatever, we just need the milliseconds):
Time date1 = initializeDate1(); //get the date from somewhere
Time date2 = initializeDate2(); //get the date from somewhere
long millis1 = date1.toMillis(true);
long millis2 = date2.toMillis(true);
long difference = millis2 - millis1 ;
//now get the days from the difference and that's it
long days = TimeUnit.MILLISECONDS.toDays(difference);
//now you can do something like
if(days == 7)
{
//do whatever when there's a week of difference
}
if(days >= 30)
{
//do whatever when it's been a month or more
}