A one size fits all plugin for vanilla servers running Minecraft Beta 1.7.3
at master 71 lines 2.2 kB view raw
1package dev.keii.goldenage.utils; 2 3import javax.annotation.Nullable; 4import java.time.*; 5import java.util.ArrayList; 6import java.util.List; 7 8public class DateUtility { 9 public static @Nullable LocalDateTime epochSecondsToDateTime(Integer epochTimeSeconds) { 10 if (epochTimeSeconds == null) { 11 return null; 12 } 13 14 Instant instant = Instant.ofEpochMilli((long) epochTimeSeconds * 1000L); 15 16 return instant.atZone(ZoneId.ofOffset("UTC", ZoneOffset.UTC)).toLocalDateTime(); 17 } 18 19 private static String getShort(String unit, boolean shortUnits) { 20 if (shortUnits) { 21 return unit.substring(0, 2).trim(); 22 } else { 23 return unit; 24 } 25 } 26 27 public static String getHumanReadableTimeSpan(long epochSeconds, boolean shortUnits) { 28 Instant now = Instant.now(); 29 Instant past = Instant.ofEpochSecond(epochSeconds); 30 Duration duration = Duration.between(past, now); 31 32 long seconds = duration.getSeconds(); 33 34 if (seconds <= 0) { 35 return "now"; 36 } 37 38 long days = seconds / 86400; 39 long hours = (seconds % 86400) / 3600; 40 long minutes = (seconds % 3600) / 60; 41 long remainingSeconds = seconds % 60; 42 43 long weeks = days / 7; 44 days = days % 7; 45 46 List<String> parts = new ArrayList<>(); 47 48 if (weeks > 0) { 49 parts.add(weeks + getShort(weeks > 1 ? " weeks" : " week", shortUnits)); 50 } 51 if (days > 0) { 52 parts.add(days + getShort(days > 1 ? " days" : " day", shortUnits)); 53 } 54 if (hours > 0) { 55 parts.add(hours + getShort(hours > 1 ? " hours" : " hour", shortUnits)); 56 } 57 if (minutes > 0 && weeks == 0) { 58 parts.add(minutes + getShort(minutes > 1 ? " minutes" : " minute", shortUnits)); 59 } 60 if (remainingSeconds > 0 && days == 0 && hours == 0 && weeks == 0) { 61 parts.add(remainingSeconds + getShort(remainingSeconds > 1 ? " seconds" : " second", shortUnits)); 62 } 63 64 if (parts.isEmpty()) { 65 return "now"; 66 } 67 68 return String.join(" ", parts.subList(0, Math.min(3, parts.size()))); 69 } 70 71}