Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Calculate the number of days between two dates [duplicate]

Tags:

java

string

date

Possible Duplicate:
Calculating the Difference Between Two Java Date Instances

In Java, I want to calculate the number of days between two dates.

In my database they are stored as a DATE datatype, but in my code they are strings.

I want to calculate the no of days between those two strings.

like image 229
sagarg Avatar asked Aug 18 '11 06:08

sagarg


1 Answers

Well to start with, you should only deal with them as strings when you have to. Most of the time you should work with them in a data type which actually describes the data you're working with.

I would recommend that you use Joda Time, which is a much better API than Date/Calendar. It sounds like you should use the LocalDate type in this case. You can then use:

int days = Days.daysBetween(date1, date2).getDays(); 
like image 75
Jon Skeet Avatar answered Sep 22 '22 04:09

Jon Skeet