Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

("21-10-2010" "31-10-2010") it is in string and tell me how to calculate days between these two dates in java?

user-image
Question added by Gourav sen gourav
Date Posted: 2014/08/12
Kayvan Tehrani
by Kayvan Tehrani , Senior Software Engineer , British Petroleum

static final long MILLI_SECONDS_IN_A_DAY =1000 *60 *60 *24;

static final String DATE_FORMAT = "dd-MM-yyyy";

public long daysBetween(String fromDateStr, String toDateStr) throws ParseException {  

SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);

Date fromDate;

Date toDate;

fromDate = format.parse(fromDateStr);

toDate = format.parse(toDateStr);

return (toDate.getTime() - fromDate.getTime()) / MILLI_SECONDS_IN_A_DAY; 

   }