1

I am co-working with a group, I want to ask how can i get the date difference from a formate of "Sat Feb 23 00:00:00 GMT 2013". The pickerfrom and to is a calendar, and getDate returns that formate. How can I get the date difference in days? any idea?

 /*   Current format  Sat Feb 23 00:00:00 GMT 2013  */
 Date date_from = pickerFrom.getDate();
 Date date_to = pickerTo.getDate();

 int date_diff = (int)((date_to)-(date_from));
user1851359
  • 139
  • 2
  • 14

2 Answers2

4

Checkout getting the difference between date in days in java

My preference would be to use Joda time - it has many useful date functions that'll make your life much easier when it comes to dates and date manipulation

Community
  • 1
  • 1
Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
1

You can get the difference in milliseconds of each date and subtract these values.

long diff = date_to.getTime() - date_from.getTime();

will return you the number of milliseconds between the two dates.

Then, you can use something like this to get the number of hours, days,... out of these milliseconds.

Community
  • 1
  • 1
Dan D.
  • 32,246
  • 5
  • 63
  • 79