Skip to content

Instantly share code, notes, and snippets.

@ILapitan
Created June 4, 2017 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ILapitan/c254b82670b9e4cd2f0708ecde7fb827 to your computer and use it in GitHub Desktop.
Save ILapitan/c254b82670b9e4cd2f0708ecde7fb827 to your computer and use it in GitHub Desktop.
Calculate the number of days between two dates in Java 1.7
//input dates
final String start = "2010-01-15";
final String end = "2011-03-18";
// parse the dates
final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
final Date startDate = dateFormat.parse( start );
final Date endDate = dateFormat.parse( end );
// calculate the number of days between start and end dates
long daysDuration = Math.round( ( endDate.getTime() - startDate.getTime() ) / (double) ( 1000 * 60 * 60 * 24 ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment