Organizational Research By

Surprising Reserch Topic

how to get android java date difference in days

android java date difference in days  using -'java,android,date'

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 want to find the difference between CURRENT DATE - yyyy/mm/dd formatted date)

asked Sep 9, 2015 by EduardoHarpo
0 votes

Related Hot Questions

5 Answers

0 votes

Not really a reliable method, better of using JodaTime

  Calendar thatDay = Calendar.getInstance();
  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

  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.

Can anyone further tell me how to tell user the number of days left for him to return books - in android programming (Java) - any help will be appreciated.

answered Sep 9, 2015 by MarWMYN
0 votes

This is NOT my work, found the answer here. did not want a broken link in the future :).

The key is this line for taking daylight setting into account, ref Full Code.


or try passing TimeZone as a parameter to daysBetween() and call setTimeZone() in the sDate and eDate objects.

So here it goes:

public static Calendar getDatePart(Date date){
    Calendar cal = Calendar.getInstance();       // get calendar instance
    cal.set(Calendar.HOUR_OF_DAY, 0);            // set hour to midnight
    cal.set(Calendar.MINUTE, 0);                 // set minute in hour
    cal.set(Calendar.SECOND, 0);                 // set second in minute
    cal.set(Calendar.MILLISECOND, 0);            // set millisecond in second

    return cal;                                  // return the date part

getDatePart() taken from here

 * This method also assumes endDate >= startDate
public static long daysBetween(Date startDate, Date endDate) {
  Calendar sDate = getDatePart(startDate);
  Calendar eDate = getDatePart(endDate);

  long daysBetween = 0;
  while (sDate.before(eDate)) {
      sDate.add(Calendar.DAY_OF_MONTH, 1);
  return daysBetween;

The Nuances: Finding the difference between two dates isn't as straightforward as subtracting the two dates and dividing the result by (24 * 60 * 60 * 1000). Infact, its erroneous!

For example: The difference between the two dates 03/24/2007 and 03/25/2007 should be 1 day; However, using the above method, in the UK, you'll get 0 days!

See for yourself (code below). Going the milliseconds way will lead to rounding off errors and they become most evident once you have a little thing like Daylight Savings Time come into the picture.

Full Code:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateTest {

public class DateTest {

static SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

public static void main(String[] args) {


  //diff between these 2 dates should be 1
  Date d1 = new Date("01/01/2007 12:00:00");
  Date d2 = new Date("01/02/2007 12:00:00");

  //diff between these 2 dates should be 1
  Date d3 = new Date("03/24/2007 12:00:00");
  Date d4 = new Date("03/25/2007 12:00:00");

  Calendar cal1 = Calendar.getInstance();cal1.setTime(d1);
  Calendar cal2 = Calendar.getInstance();cal2.setTime(d2);
  Calendar cal3 = Calendar.getInstance();cal3.setTime(d3);
  Calendar cal4 = Calendar.getInstance();cal4.setTime(d4);

  printOutput("Manual   ", d1, d2, calculateDays(d1, d2));
  printOutput("Calendar ", d1, d2, daysBetween(cal1, cal2));
  printOutput("Manual   ", d3, d4, calculateDays(d3, d4));
  printOutput("Calendar ", d3, d4, daysBetween(cal3, cal4));

private static void printOutput(String type, Date d1, Date d2, long result) {
  System.out.println(type+ "- Days between: " + sdf.format(d1)
                    + " and " + sdf.format(d2) + " is: " + result);

/* This method is used to find the no of days between the given dates */
public static long calculateDays(Date dateEarly, Date dateLater) {
  return (dateLater.getTime() - dateEarly.getTime()) / (24 * 60 * 60 * 1000);

/** Using Calendar - THE CORRECT WAY**/
public static long daysBetween(Date startDate, Date endDate) {


Manual - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1

Calendar - Days between: 01-Jan-2007 and 02-Jan-2007 is: 1

Manual - Days between: 24-Mar-2007 and 25-Mar-2007 is: 0

Calendar - Days between: 24-Mar-2007 and 25-Mar-2007 is: 1

answered Sep 9, 2015 by ThelmaNewkir
0 votes

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= 

And that's it!

answered Sep 9, 2015 by Isidro0407e
0 votes

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

answered Sep 9, 2015 by Shanice96Rma
0 votes

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));
answered Sep 9, 2015 by OdetteCambel