i am posting after a very long time and i am sure that this is a simple one for you out there
I would like to know the difference between two dates in terms of days, i.e I have one String which contains date format string, and another java Date format date object The format is mm/dd/yyyy and this can change in any format. i.e dd/mm/yyyy, dd/mm/yy, or any other format. I would like to know how to convert this sting into Date and find the difference among these two(string date and the date object) in terms of days.
For the date difference, use Calendar. It has methods to get the day of the year, and the year. If the years are the same, just return the difference between the two days of the year. If not, add the difference in years in days to it. You can use Calendar.getActualMaximum to find out how many days a year has.
For example, the difference between 01-01-2008 and 01-02-2010 (US style, should be 366 + 365 + 31). Day of the year is 1 for the first, 32 for the second. That's where you get the 31 from. 2008 has 366 days so you add those as well. 2009 has 365 days so you add those as well.s
Another example, the difference between 01-02-2008 and 01-01-2010. Days of the year are 32 and 1, so the difference is -31. Add 366 again, add 365 again, and you're set.
I'm sure you can convert this into code Keep in mind though that it's also possible that the first date is after the second date.