Android/Java - Date Difference in days
Asked Answered
R

19

81

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)

Rawinsonde answered 1/10, 2010 at 10:59 Comment(2)
This code uses troublesome old date-time classes now supplanted by the java.time classes. For older Java and Android, see the ThreeTen-Backport and ThreeTenABP projects.Squint
Similar Question, but using moments rather than whole dates: date difference in days, in AndroidSquint
U
126

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.

Ung answered 1/10, 2010 at 11:18 Comment(12)
@stOle thanx , but i am having both the date in strings, so how do i do it, pls let me know in detail, plsRawinsonde
@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 supportRawinsonde
@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.Ung
@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 exactlyRawinsonde
@stOle Running successfully after setting "MM" for month, really great support by you and it is very quickly, really really great helpRawinsonde
@Gevorg, I did recommend it. :) Me Gusta JodaTimeUng
I posted a solution below that uses the Java Date built-in classes and doesn't require any external library.Cassidy
You also need to take into account the fact that in Calendar.MONTH the January is represented with the month 0 not 1 :)Duke
Sometimes this code will be a day off due to rounding problems (lack thereof) when dividing the milliseconds. This works for me: Math.round(millisBetweenDates * 1f / TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));Belle
is getTimeInMillis worked on dates under 1970? (example 1955, 21 June)Muslim
This is really perfect onePumpernickel
FYI, the troublesome old date-time classes such as java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat are now legacy, supplanted by the java.time classes. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in the ThreeTenABP project. See How to use ThreeTenABP….Squint
H
83

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.

TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

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.setTime(date);      
    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);
      daysBetween++;
  }
  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) {

  TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

  //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));
  System.out.println("---");
  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);
}

/** Manual Method - YIELDS INCORRECT RESULTS - DO NOT USE**/
/* 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) {
  ...
}

OUTPUT:

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

Highchair answered 1/10, 2010 at 10:59 Comment(6)
Agree. Using top level methods You are getting more reliable and elegant solutions. Thanks!Eddy
For the method: daysBetween if date is 15:00 on 24 July 2012 and endDate is 16:00 on 24 July 2012 - then date is before endDate, HOWEVER not by a whole day, but just by one hour. Am I missing something or would the result from daysBetween be wrong for this case (as the expected result is zero but with the given calculation should result in 1 rather than zero) ?Netherlands
@Zainodis, Over the top of my head, have i updated the code. i guess this should resolved the issue.Highchair
@SamQuest Thanks for updating! I took a more naive approach: The while loop with sDate.before(eDate) is stopped and the result returned, if start and end are on the same day, month and year. this also ensures, that if in the first iteration start and end are on the same day/month/year (despite time-wise start being before end) that zero is correctly returned.Netherlands
You sir, deserve a Bells!Meatman
If you want to compare dates while discounting the year, such as to tell how many days until a birthday or anniversary, add cal.set(Calendar.YEAR, Calendar.YEAR); to getDatePart. And add if (sDate.after(eDate)) {return -1;} in daysBetween after the line Calendar eDate = getDatePart(endDate); the change to daysBetween is to return a flag of -1, indicating that the date has already passed in the year.Deason
F
38

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!

Forge answered 29/9, 2013 at 15:53 Comment(5)
This too worked for me.. Others were way too complex and way to accurate :)Gpo
Would be better if you subtracted first and divided later to prevent dividing twice.Batangas
@Batangas Doing that gives a difference of +1 if the startDate is less than the endDate. There's a +1 difference in this case. Could be solved by adding -1 to the answer.Narayan
@Narayan how? What I'm saying is that you should do (t1-t2)/C instead of t1/C - t2/C. Since neither t1/C nor t2/C will be zero, I can't see how that would affect the answer.Batangas
@Batangas I understood what you're trying to say and wondered the same when I read this answer. Only after implementation I noticed why it wasn't done that way (the reason I mentioned above).Narayan
O
25

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

There is also an android specific JodaTime library.

Ottava answered 1/10, 2010 at 11:46 Comment(4)
thanx for the support, but not happy to use other API if it is done with the Android/JAVA codeRawinsonde
+1 for Joda. The Java Calendar API is a terrible mess and Joda is clean and beautiful.Crescin
JodaTime give several bugs with several devices in Android, I don't know why, I have several problems with thatKusin
Joda time library will add 4744 methods to your project. Choose wisely if you want to avoid the 65K methods limit.Mcdaniel
D
14

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));
}
Dried answered 21/2, 2012 at 22:2 Comment(0)
V
5

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);
        }
Velvetvelveteen answered 11/10, 2013 at 10:7 Comment(1)
@PareshMayani just checking in log catVelvetvelveteen
S
4

tl;dr

ChronoUnit.DAYS.between( 
    LocalDate.parse( "1999-12-28" ) , 
    LocalDate.parse( "12/31/1999" , DateTimeFormatter.ofPattern( "MM/dd/yyyy" ) ) 
)

Details

Other answers are outdated. The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.

java.time

The Joda-Time project was highly successful as a replacement for those old classes. These classes provided the inspiration for the java.time framework built into Java 8 and later.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

LocalDate

The LocalDate class represents a date-only value without time-of-day and without time zone.

Parsing strings

If your input strings are in standard ISO 8601 format, the LocalDate class can directly parse the string.

LocalDate start = LocalDate.parse( "1999-12-28" );

If not in ISO 8601 format, define a formatting pattern with DateTimeFormatter.

String input = "12/31/1999";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM/dd/yyyy" );
LocalDate stop = LocalDate.parse( input , formatter );

Elapsed days via ChronoUnit

Now get a count of days elapsed between that pair of LocalDate objects. The ChronoUnit enum calculates elapsed time.

long totalDays = ChronoUnit.DAYS.between( start , stop ) ; 

If you are unfamiliar with Java enums, know they are far more powerful and useful that conventional enums in most other programming languages. See the Enum class doc, the Oracle Tutorial, and Wikipedia to learn more.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Squint answered 27/6, 2016 at 17:25 Comment(2)
java.time.LocalDate is not supported in AndroidDisconcerted
@MahdiAstanei Reread my third paragraph about the ThreeTenABP library for Android. Well-worth adding to your app as the old date-time classes really are that bad.Squint
I
3

The Correct Way from Sam Quest's answer only works if the first date is earlier than the second. Moreover, it will return 1 if the two dates are within a single day.

This is the solution that worked best for me. Just like most other solutions, it would still show incorrect results on two days in a year because of wrong day light saving offset.

private final static long MILLISECS_PER_DAY = 24 * 60 * 60 * 1000;

long calculateDeltaInDays(Calendar a, Calendar b) {

    // Optional: avoid cloning objects if it is the same day
    if(a.get(Calendar.ERA) == b.get(Calendar.ERA) 
            && a.get(Calendar.YEAR) == b.get(Calendar.YEAR)
            && a.get(Calendar.DAY_OF_YEAR) == b.get(Calendar.DAY_OF_YEAR)) {
        return 0;
    }
    Calendar a2 = (Calendar) a.clone();
    Calendar b2 = (Calendar) b.clone();
    a2.set(Calendar.HOUR_OF_DAY, 0);
    a2.set(Calendar.MINUTE, 0);
    a2.set(Calendar.SECOND, 0);
    a2.set(Calendar.MILLISECOND, 0);
    b2.set(Calendar.HOUR_OF_DAY, 0);
    b2.set(Calendar.MINUTE, 0);
    b2.set(Calendar.SECOND, 0);
    b2.set(Calendar.MILLISECOND, 0);
    long diff = a2.getTimeInMillis() - b2.getTimeInMillis();
    long days = diff / MILLISECS_PER_DAY;
    return Math.abs(days);
}
Important answered 31/1, 2012 at 16:34 Comment(0)
M
3

best and easiest way to do this

  public int getDays(String begin) throws ParseException {
     long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
     SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

    long begin = dateFormat.parse(begin).getTime();
    long end = new Date().getTime(); // 2nd date want to compare
    long diff = (end - begin) / (MILLIS_PER_DAY);
    return (int) diff;
}
Mountainous answered 4/2, 2016 at 13:18 Comment(0)
E
2

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;
    }
Esque answered 1/10, 2010 at 11:2 Comment(0)
R
2

There's a simple solution, that at least for me, is the only feasible solution.

The problem is that all the answers I see being tossed around - using Joda, or Calendar, or Date, or whatever - only take the amount of milliseconds into consideration. They end up counting the number of 24-hour cycles between two dates, rather than the actual number of days. So something from Jan 1st 11pm to Jan 2nd 1am will return 0 days.

To count the actual number of days between startDate and endDate, simply do:

// Find the sequential day from a date, essentially resetting time to start of the day
long startDay = startDate.getTime() / 1000 / 60 / 60 / 24;
long endDay = endDate.getTime() / 1000 / 60 / 60 / 24;

// Find the difference, duh
long daysBetween = endDay - startDay;

This will return "1" between Jan 2nd and Jan 1st. If you need to count the end day, just add 1 to daysBetween (I needed to do that in my code since I wanted to count the total number of days in the range).

This is somewhat similar to what Daniel has suggested but smaller code I suppose.

Rowdy answered 18/12, 2012 at 15:32 Comment(0)
R
2

All of these solutions suffer from one of two problems. Either the solution isn't perfectly accurate due to rounding errors, leap days and seconds, etc. or you end up looping over the number of days in between your two unknown dates.

This solution solves the first problem, and improves the second by a factor of roughly 365, better if you know what your max range is.

/**
 * @param thisDate
 * @param thatDate
 * @param maxDays
 *            set to -1 to not set a max
 * @returns number of days covered between thisDate and thatDate, inclusive, i.e., counting both
 *          thisDate and thatDate as an entire day. Will short out if the number of days exceeds
 *          or meets maxDays
 */
public static int daysCoveredByDates(Date thisDate, Date thatDate, int maxDays) {
    //Check inputs
    if (thisDate == null || thatDate == null) {
        return -1;
    }

    //Set calendar objects
    Calendar startCal = Calendar.getInstance();
    Calendar endCal = Calendar.getInstance();
    if (thisDate.before(thatDate)) {
        startCal.setTime(thisDate);
        endCal.setTime(thatDate);
    }
    else {
        startCal.setTime(thatDate);
        endCal.setTime(thisDate);
    }

    //Get years and dates of our times.
    int startYear = startCal.get(Calendar.YEAR);
    int endYear = endCal.get(Calendar.YEAR);
    int startDay = startCal.get(Calendar.DAY_OF_YEAR);
    int endDay = endCal.get(Calendar.DAY_OF_YEAR);

    //Calculate the number of days between dates.  Add up each year going by until we catch up to endDate.
    while (startYear < endYear && maxDays >= 0 && endDay - startDay + 1 < maxDays) {
        endDay += startCal.getActualMaximum(Calendar.DAY_OF_YEAR); //adds the number of days in the year startDate is currently in
        ++startYear;
        startCal.set(Calendar.YEAR, startYear); //reup the year
    }
    int days = endDay - startDay + 1;

    //Honor the maximum, if set
    if (maxDays >= 0) {
        days = Math.min(days, maxDays);
    }
    return days;
}

If you need days between dates (uninclusive of the latter date), just get rid of the + 1 when you see endDay - startDay + 1.

Recrimination answered 26/1, 2015 at 20:35 Comment(0)
H
1

One another way:

public static int numberOfDaysBetweenDates(Calendar fromDay, Calendar toDay) {
        fromDay = calendarStartOfDay(fromDay);
        toDay = calendarStartOfDay(toDay);
        long from = fromDay.getTimeInMillis();
        long to = toDay.getTimeInMillis();
        return (int) TimeUnit.MILLISECONDS.toDays(to - from);
    }
Hartzke answered 8/12, 2015 at 8:45 Comment(1)
please give a comment about the code you provided. so people understand the meaning of your code.Insentient
N
1
        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));
Noncontributory answered 26/11, 2016 at 8:44 Comment(0)
A
1

use these functions

    public static int getDateDifference(int previousYear, int previousMonthOfYear, int previousDayOfMonth, int nextYear, int nextMonthOfYear, int nextDayOfMonth, int differenceToCount){
    // int differenceToCount = can be any of the following
    //  Calendar.MILLISECOND;
    //  Calendar.SECOND;
    //  Calendar.MINUTE;
    //  Calendar.HOUR;
    //  Calendar.DAY_OF_MONTH;
    //  Calendar.MONTH;
    //  Calendar.YEAR;
    //  Calendar.----

    Calendar previousDate = Calendar.getInstance();
    previousDate.set(Calendar.DAY_OF_MONTH, previousDayOfMonth);
    // month is zero indexed so month should be minus 1
    previousDate.set(Calendar.MONTH, previousMonthOfYear);
    previousDate.set(Calendar.YEAR, previousYear);

    Calendar nextDate = Calendar.getInstance();
    nextDate.set(Calendar.DAY_OF_MONTH, previousDayOfMonth);
    // month is zero indexed so month should be minus 1
    nextDate.set(Calendar.MONTH, previousMonthOfYear);
    nextDate.set(Calendar.YEAR, previousYear);

    return getDateDifference(previousDate,nextDate,differenceToCount);
}
public static int getDateDifference(Calendar previousDate,Calendar nextDate,int differenceToCount){
    // int differenceToCount = can be any of the following
    //  Calendar.MILLISECOND;
    //  Calendar.SECOND;
    //  Calendar.MINUTE;
    //  Calendar.HOUR;
    //  Calendar.DAY_OF_MONTH;
    //  Calendar.MONTH;
    //  Calendar.YEAR;
    //  Calendar.----

    //raise an exception if previous is greater than nextdate.
    if(previousDate.compareTo(nextDate)>0){
        throw new RuntimeException("Previous Date is later than Nextdate");
    }

    int difference=0;
    while(previousDate.compareTo(nextDate)<=0){
        difference++;
        previousDate.add(differenceToCount,1);
    }
    return difference;
}
Ankney answered 29/11, 2017 at 6:59 Comment(3)
This code uses troublesome old date-time classes now supplanted by the java.time classes. For older Java and Android, see the ThreeTen-Backport and ThreeTenABP projects.Squint
Is calendar class an old date-time classes?Ankney
Yes, any date-time related class found outside the java.time package is now legacy and should be avoided. This includes Date and Calendar, and the java.sql classes. See the Oracle Tutorial.Squint
N
1
        public void dateDifferenceExample() {

        // Set the date for both of the calendar instance
        GregorianCalendar calDate = new GregorianCalendar(2012, 10, 02,5,23,43);
        GregorianCalendar cal2 = new GregorianCalendar(2015, 04, 02);

        // Get the represented date in milliseconds
        long millis1 = calDate.getTimeInMillis();
        long millis2 = cal2.getTimeInMillis();

        // Calculate difference in milliseconds
        long diff = millis2 - millis1;

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

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

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

        // Calculate difference in days
        long diffDays = diff / (24 * 60 * 60 * 1000);
    Toast.makeText(getContext(), ""+diffSeconds, Toast.LENGTH_SHORT).show();


}
Norri answered 7/1, 2019 at 7:34 Comment(0)
C
0

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
}
Cassidy answered 23/6, 2012 at 18:30 Comment(0)
H
0

Joda-Time

Best way is to use Joda-Time, the highly successful open-source library you would add to your project.

String date1 = "2015-11-11";
String date2 = "2013-11-11";
DateTimeFormatter formatter = new DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime d1 = formatter.parseDateTime(date1);
DateTime d2 = formatter.parseDateTime(date2);
long diffInMillis = d2.getMillis() - d1.getMillis();

Duration duration = new Duration(d1, d2);
int days = duration.getStandardDays();
int hours = duration.getStandardHours();
int minutes = duration.getStandardMinutes();

If you're using Android Studio, very easy to add joda-time. In your build.gradle (app):

dependencies {
  compile 'joda-time:joda-time:2.4'
  compile 'joda-time:joda-time:2.4'
  compile 'joda-time:joda-time:2.2'
}
Haematoxylin answered 11/12, 2015 at 21:1 Comment(3)
Good answer. Note that calling toString on Duration generates a String representation in one of the ISO 8601 standard formats, PnYnMnDTnHnMnS . The P marks the beginning, while the T separates the year-month-days from the hour-minutes-seconds. So P3D is three days, and P3DT12H is three and a half days.Squint
Joda time library will add 4744 methods to your project. Choose wisely if you want to avoid the 65K methods limit.Mcdaniel
This needs to be changed to DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd")Usufruct
T
0

Convert dates to milliseconds long number, for example by set calendar fields: YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, MINUTE, SECOND and may be MILLISECOND.

calendar.set( Calendar.SECOND, 0 ); // and so on
long ms0 = calendar.getTimeInMillis();

Calculate diffirents:

long ms = Math.abs( ms0 - ms1);

Set to calendar and get value:

Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone( "Etc/GMT+0" )); // !
calendar.setTimeInMillis( ms );

int days = calendar.get( Calendar.DAY_OF_YEAR ) - 1; // max value 365 but you can increase if use field YEAR
Tameika answered 13/1 at 10:50 Comment(1)
Suggesting the use of Calendar class in 2024 is poor advice. That terribly flawed class was years ago supplanted by the modern java.time classes defined in JSR 310. Specifically replaced by ZonedDateTime class.Squint

© 2022 - 2024 — McMap. All rights reserved.