Skip to content

Instantly share code, notes, and snippets.

@wallace7souza
Created February 6, 2021 16:04
Show Gist options
  • Save wallace7souza/4db1439d732cf49b65b418113a9d72a3 to your computer and use it in GitHub Desktop.
Save wallace7souza/4db1439d732cf49b65b418113a9d72a3 to your computer and use it in GitHub Desktop.
Using java.util.Date to Find the Difference in Days
public void givenTwoDatesBeforeJava8_whenDifferentiating_thenWeGetSix()
throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
Date firstDate = sdf.parse("06/24/2017");
Date secondDate = sdf.parse("06/30/2017");
long diffInMillies = Math.abs(secondDate.getTime() - firstDate.getTime());
long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
assertEquals(6, diff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment