From 4da4bbb40046d28397afa6bc608a689f26b7ab57 Mon Sep 17 00:00:00 2001 From: DevDeborah Date: Wed, 10 Jan 2024 19:47:53 -0500 Subject: [PATCH] Add new time formatter tool --- .../framework/tools/TimeFormatter.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/no/runsafe/framework/tools/TimeFormatter.java diff --git a/src/no/runsafe/framework/tools/TimeFormatter.java b/src/no/runsafe/framework/tools/TimeFormatter.java new file mode 100644 index 00000000..29b86fc8 --- /dev/null +++ b/src/no/runsafe/framework/tools/TimeFormatter.java @@ -0,0 +1,114 @@ +package no.runsafe.framework.tools; + +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneId; + +public class TimeFormatter +{ + public static String formatDuration(Duration duration) + { + if (duration == null) + return "Invalid Time"; + + return formatMillis(duration.toMillis()); + } + + public static String formatInstant(Instant instant) + { + if (instant == null) + return "Invalid Time"; + + return formatMillis(Duration.between(instant, Instant.now()).toMillis()); + } + + public static String formatDate(Instant date) + { + if (date == null) + return "Invalid Date"; + + return date.atZone(ZoneId.systemDefault()).toString().replace("T", " ").substring(0,16); + } + + private static String formatMillis(long millis) + { + if (millis < SECOND_MILLISECONDS) + return "Less than one second"; + + long millisOriginalValue = millis; + String timeReturn = ""; + + if (millis >= YEAR_MILLISECONDS) // Years + { + int years = (int) (millis / YEAR_MILLISECONDS); + millis -= (years * YEAR_MILLISECONDS); + + timeReturn += (" " + years + " year"); + if (years != 1) + timeReturn += "s"; + } + if (millis >= MONTH_MILLISECONDS) // Months + { + int months = (int) (millis / MONTH_MILLISECONDS); + millis -= (months * MONTH_MILLISECONDS); + + timeReturn += (" " + months + " month"); + if (months != 1) + timeReturn += "s"; + } + if (millis >= WEEK_MILLISECONDS) // Weeks + { + int weeks = (int) (millis / WEEK_MILLISECONDS); + millis -= (weeks * WEEK_MILLISECONDS); + + timeReturn += (" " + weeks + " week"); + if (weeks != 1) + timeReturn += "s"; + } + if (millis >= DAY_MILLISECONDS) // Days + { + int days = (int) (millis / DAY_MILLISECONDS); + millis -= (days * DAY_MILLISECONDS); + + timeReturn += (" " + days + " day"); + if (days != 1) + timeReturn += "s"; + } + if (millis >= HOUR_MILLISECONDS) // Hours + { + int hours = (int) (millis / HOUR_MILLISECONDS); + millis -= (hours * HOUR_MILLISECONDS); + + timeReturn += (" " + hours + " hour"); + if (hours != 1) + timeReturn += "s"; + } + if (millis >= MINUTE_MILLISECONDS) // Minutes + { + int minutes = (int) (millis / MINUTE_MILLISECONDS); + millis -= (minutes * MINUTE_MILLISECONDS); + + timeReturn += (" " + minutes + " minute"); + if (minutes != 1) + timeReturn += "s"; + } + if (millisOriginalValue < 5 * MINUTE_MILLISECONDS) // Only display seconds if total time is less than 5 minutes + { + int seconds = (int) (millis / SECOND_MILLISECONDS); + + timeReturn += (" " + seconds + " second"); + if (seconds != 1) + timeReturn += "s"; + } + + return timeReturn.substring(1); // Get rid of first space + } + + private static final long YEAR_MILLISECONDS = 31536000000L; // 365 days + private static final long MONTH_MILLISECONDS = 2628000000L; // 1/12 of a year + private static final long WEEK_MILLISECONDS = 604800000L; + private static final long DAY_MILLISECONDS = 86400000L; + private static final long HOUR_MILLISECONDS = 3600000L; + private static final long MINUTE_MILLISECONDS = 60000L; + private static final long SECOND_MILLISECONDS = 1000L; +}