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)
This fragment accounts for daylight savings time and is O(1).
use these functions
Joda-Time
Best way is to use Joda-Time, the highly successful open-source library you would add to your project.
If you're using Android Studio, very easy to add joda-time. In your build.gradle (app):
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.
If you need days between dates (uninclusive of the latter date), just get rid of the
+ 1
when you seeendDay - startDay + 1
.One another way:
Use jodatime API
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.