Created
February 6, 2021 16:04
-
-
Save wallace7souza/4db1439d732cf49b65b418113a9d72a3 to your computer and use it in GitHub Desktop.
Using java.util.Date to Find the Difference in Days
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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