From 6c53c772c7f96da1775acf9314be10cc7df53f5b Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Tue, 18 Jul 2023 20:27:40 +1000 Subject: Museum page (#773) Co-authored-by: nopo --- .../moulberry/notenoughupdates/util/Utils.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java index 54888935..7f607ab7 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java @@ -85,11 +85,16 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.text.DecimalFormat; import java.time.Duration; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.TimeZone; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -2257,4 +2262,25 @@ public class Utils { slot, 0, 0, Minecraft.getMinecraft().thePlayer ); } + + public static String timeSinceMillisecond(long time) { + Instant lastSave = Instant.ofEpochMilli(time); + LocalDateTime lastSaveTime = LocalDateTime.ofInstant(lastSave, TimeZone.getDefault().toZoneId()); + long timeDiff = System.currentTimeMillis() - lastSave.toEpochMilli(); + LocalDateTime sinceOnline = LocalDateTime.ofInstant(Instant.ofEpochMilli(timeDiff), ZoneId.of("UTC")); + String renderText; + + if (timeDiff < 60000L) { + renderText = sinceOnline.getSecond() + " seconds ago"; + } else if (timeDiff < 3600000L) { + renderText = sinceOnline.getMinute() + " minutes ago"; + } else if (timeDiff < 86400000L) { + renderText = sinceOnline.getHour() + " hours ago"; + } else if (timeDiff < 31556952000L) { + renderText = sinceOnline.getDayOfYear() + " days ago"; + } else { + renderText = lastSaveTime.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")); + } + return renderText; + } } -- cgit