Skip to content

Instantly share code, notes, and snippets.

@abdurahmanadilovic
Created January 20, 2018 08:16
Show Gist options
  • Save abdurahmanadilovic/978c8b8d1eefb3f510f910b50103672a to your computer and use it in GitHub Desktop.
Save abdurahmanadilovic/978c8b8d1eefb3f510f910b50103672a to your computer and use it in GitHub Desktop.
Minutes ago helper function for Calendar objects
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