Created
January 20, 2018 08:16
-
-
Save abdurahmanadilovic/978c8b8d1eefb3f510f910b50103672a to your computer and use it in GitHub Desktop.
Minutes ago helper function for Calendar objects
This file contains hidden or 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 static long minutesAgo(Calendar before, Calendar after){ | |
long diff = after.getTimeInMillis() - before.getTimeInMillis(); | |
return TimeUnit.MILLISECONDS.toMinutes(diff); | |
} | |
// junit test for the method above | |
@Test | |
public void testThreeMinutesAgo() throws Exception { | |
Calendar now = Calendar.getInstance(); | |
Calendar threeMinutesAgo = (Calendar) now.clone(); | |
threeMinutesAgo.add(Calendar.MINUTE, -3); | |
assertEquals("3 minutes ago", minutesAgo(threeMinutesAgo, now)+ " minutes ago"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment