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)
tl;dr
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.If not in ISO 8601 format, define a formatting pattern with
DateTimeFormatter
.Elapsed days via
ChronoUnit
Now get a count of days elapsed between that pair of
LocalDate
objects. TheChronoUnit
enum calculates elapsed time.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.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 todaysBetween()
and callsetTimeZone()
in thesDate
andeDate
objects.So here it goes:
getDatePart() taken from here
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:
OUTPUT:
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.
Use the following functions: