diff options
| author | Cow <cow@volloeko.de> | 2020-07-01 02:14:58 +0200 |
|---|---|---|
| committer | Cow <cow@volloeko.de> | 2020-07-01 02:14:58 +0200 |
| commit | a54f518faf6d79be4bfd5ec218d251734bb123f1 (patch) | |
| tree | 0fee9317fd3e1653c80f1f53bfc693b40bf9535d /src/main/java/eu/olli/cowmoonication/util | |
| parent | d1b0389bf09f984e8f888cd875438b8bbe3d125f (diff) | |
| download | Cowlection-a54f518faf6d79be4bfd5ec218d251734bb123f1.tar.gz Cowlection-a54f518faf6d79be4bfd5ec218d251734bb123f1.tar.bz2 Cowlection-a54f518faf6d79be4bfd5ec218d251734bb123f1.zip | |
Added item age tooltips
Diffstat (limited to 'src/main/java/eu/olli/cowmoonication/util')
| -rw-r--r-- | src/main/java/eu/olli/cowmoonication/util/Utils.java | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/main/java/eu/olli/cowmoonication/util/Utils.java b/src/main/java/eu/olli/cowmoonication/util/Utils.java index 22f32d3..8d846f3 100644 --- a/src/main/java/eu/olli/cowmoonication/util/Utils.java +++ b/src/main/java/eu/olli/cowmoonication/util/Utils.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.time.DurationFormatUtils; import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.text.DecimalFormat; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; @@ -38,7 +39,7 @@ public final class Utils { * @param timestamp last login/logout * @return 1st: duration between timestamp and now in words; 2nd: formatted date if time differences is >24h, otherwise null */ - public static Pair<String, String> getLastOnlineWords(long timestamp) { + public static Pair<String, String> getDurationAsWords(long timestamp) { long duration = System.currentTimeMillis() - timestamp; long daysPast = TimeUnit.MILLISECONDS.toDays(duration); @@ -58,6 +59,32 @@ public final class Utils { } } + public static String getDurationAsWord(long timestamp) { + long duration = System.currentTimeMillis() - timestamp; + long secondsPast = TimeUnit.MILLISECONDS.toSeconds(duration); + if (secondsPast < 60) { + return secondsPast + " second" + (secondsPast > 1 ? "s" : ""); + } + long minutesPast = TimeUnit.SECONDS.toMinutes(secondsPast); + if (minutesPast < 60) { + return minutesPast + " minute" + (minutesPast > 1 ? "s" : ""); + } + long hoursPast = TimeUnit.MINUTES.toHours(minutesPast); + if (hoursPast < 24) { + return hoursPast + " hour" + (hoursPast > 1 ? "s" : ""); + } + long daysPast = TimeUnit.HOURS.toDays(hoursPast); + if (daysPast < 31) { + return daysPast + " day" + (daysPast > 1 ? "s" : ""); + } + double monthsPast = daysPast / 30.5d; + if (monthsPast < 12) { + return new DecimalFormat("0.#").format(monthsPast) + " month" + (monthsPast >= 2 ? "s" : ""); + } + double yearsPast = monthsPast / 12d; + return new DecimalFormat("0.#").format(yearsPast) + " year" + (yearsPast >= 2 ? "s" : ""); + } + public static String toRealPath(Path path) { try { return path.toRealPath().toString(); |
