Skip to content

Instantly share code, notes, and snippets.

@xlbruce
Created July 5, 2016 20:40
Show Gist options
  • Save xlbruce/9426f66150fd6da1d856ccc58a37e00a to your computer and use it in GitHub Desktop.
Save xlbruce/9426f66150fd6da1d856ccc58a37e00a to your computer and use it in GitHub Desktop.
Get a human readable String representing a period between 2 dates
private static String getStringDuration(Temporal startDate, Temporal endDate){
Duration d = Duration.between(startDate, endDate);
StringBuilder str = new StringBuilder();
long total = d.getSeconds();
long days = total / DAYS;
if (days > 0) {
str.append(days)
.append("d ");
total -= days * DAYS;
}
long hours = total / HOURS;
if (hours > 0) {
str.append(hours)
.append("h ");
total -= hours * HOURS;
}
long minutes = total / MINUTES;
str.append(minutes)
.append("m");
return str.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment