Created
July 5, 2016 20:40
-
-
Save xlbruce/9426f66150fd6da1d856ccc58a37e00a to your computer and use it in GitHub Desktop.
Get a human readable String representing a period between 2 dates
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
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