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)
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
andendDate
, simply do: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.
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):
Most of the answers were good and right for your problem of
I suggest this very simple and straightforward approach that is guaranteed to give you the correct difference in any time zone:
And that's it!
This is Simple and best calculation for me and may be for you.
best and easiest way to do this
Not really a reliable method, better of using JodaTime
Here's an approximation...
To Parse the date from a string, you could use
Although, since you're sure of the date format... You Could also do
Integer.parseInt()
on it's Substrings to obtain their numeric values.