tags:

views:

104

answers:

3

Hello 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 " +
        android.text.format.DateFormat.getDateFormat(this).format(new Date()));

and i am having another date in format as: 2010-08-25 (i.e. yyyy/mm/dd) ,

so i want to find the difference between date in number of days, how do i find difference in days?

(In other words, i wants to find the difference between CURRENT DATE - yyyy/mm/dd formatted date)

+1  A: 

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;
    }
Daniel
+2  A: 

Not really a reliable method, better of using JodaTime

        Calendar thatDay = Calendar.getInstance();
  thatDay.set(Calendar.DAY_OF_MONTH,25);
  thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
  thatDay.set(Calendar.YEAR, 1985);

  Calendar today = Calendar.getInstance();

  long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

Here's an approximation...

long days = diff / (24 * 60 * 60 * 1000);

To Parse the date from a string, you could use

        String strThatDay = "1985/08/25";
  SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
  Date d = null;
  try {
   d = formatter.parse(strThatDay);//catch exception
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } 


  Calendar thatDay = Calendar.getInstance();
  thatDay.setTime(d); //rest is the same....

Although, since you're sure of the date format... You Could also do Integer.parseInt() on it's Substrings to obtain their numeric values.

st0le
@stOle thanx , but i am having both the date in strings, so how do i do it, pls let me know in detail, pls
PM - Paresh Mayani
@paresh, updated!
st0le
@stOle not getting the exact answer, may be small mistake in your code, i am getting 274 days gap even i set String strThatDay = "2010/10/03";, it should be only 1 day , thanx for the support
PM - Paresh Mayani
@Paresh, i'm so sorry, the `("yyyy/mm/dd");` should be replaced by `("yyyy/MM/dd");` It's Capital M for Month, Lowercase for Minutes. Corrected.
st0le
@stOle but if i statically set the both dates as // thatDay.set(2007, 10, 03); // today.set(2007, 10, 04); then it is giving me 1 day difference exactly
PM - Paresh Mayani
@stOle Running successfully after setting "MM" for month, really great support by you and it is very quickly, really really great help
PM - Paresh Mayani
A: 

Use jodatime API

Days.daysBetween(start.toDateMidnight() , end.toDateMidnight() ).getDays() 

where 'start' and 'end' are your DateTime objects. To parse your date Strings into DateTime objects use the parseDateTime method

Jeroen Rosenberg
@Jeroen thanx for the support, but not happy to use other API if it is done with the Android/JAVA code
PM - Paresh Mayani