From db6040115ad6503cf5ca8ad17dc2c8a5a619cd50 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Fri, 4 Oct 2024 22:35:43 +0700 Subject: [PATCH] :sparkles: feat: add constant time format #3 --- .../groovy/org/unify4j/common/Time4j.java | 41 ++++++++++++++++++ .../org/unify4j/text/TimeFormatText.java | 43 ++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/groovy/org/unify4j/common/Time4j.java b/plugin/src/main/groovy/org/unify4j/common/Time4j.java index 745b023..8a2c492 100644 --- a/plugin/src/main/groovy/org/unify4j/common/Time4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/Time4j.java @@ -1344,6 +1344,47 @@ public static String since(Date source, Date target) { } } + /** + * Formats the given epoch time into a human-readable "time ago" format. + * Examples: + * - "Just now" for times within a minute. + * - "X seconds ago," "X minutes ago," "X hours ago," "X days ago." + * + * @param epochMilli The epoch time in milliseconds. + * @return A string representing the time difference. + */ + public static String since(long epochMilli) { + Instant now = Instant.now(); + Instant instant = Instant.ofEpochMilli(epochMilli); + LocalDate _now = LocalDate.now(); + LocalDate local = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).toLocalDate(); + // Calculate duration for seconds, minutes, hours, days + Duration duration = Duration.between(instant, now); + long seconds = duration.getSeconds(); + long minutes = duration.toMinutes(); + long hours = duration.toHours(); + long days = duration.toDays(); + + // Calculate period for months and years + Period period = Period.between(local, _now); + int months = period.getMonths(); + int years = period.getYears(); + + if (seconds < 60) { + return seconds <= 1 ? "Just now" : seconds + " seconds ago"; + } else if (minutes < 60) { + return minutes == 1 ? "1 minute ago" : minutes + " minutes ago"; + } else if (hours < 24) { + return hours == 1 ? "1 hour ago" : hours + " hours ago"; + } else if (days < 30) { + return days == 1 ? "1 day ago" : days + " days ago"; + } else if (months < 12) { + return months == 1 ? "1 month ago" : months + " months ago"; + } else { + return years == 1 ? "1 year ago" : years + " years ago"; + } + } + /** * Provides the difference in milliseconds between the given dates. * diff --git a/plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java b/plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java index f499b5e..dea7339 100755 --- a/plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java +++ b/plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java @@ -182,5 +182,46 @@ public class TimeFormatText { * Description: Pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone offset. */ public static final String ISO_DATE_TIME_WITH_TIMEZONE_OFFSET = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; -} + /** + * Format: dd.MM.yyyy + * Description: Custom pattern for date only including day, month, and year with period as separator. + */ + public static final String CUSTOM_DATE_DOT_PATTERN = "dd.MM.yyyy"; + + /** + * Format: HH.mm + * Description: Custom short pattern for time only including hour and minutes with period as separator. + */ + public static final String CUSTOM_TIME_HH_MM_DOT_PATTERN = "HH.mm"; + + /** + * Format: HH.mm.ss + * Description: Custom short pattern for time including hour, minutes, and seconds with period as separator. + */ + public static final String CUSTOM_TIME_HH_MM_SS_DOT_PATTERN = "HH.mm.ss"; + + /** + * Format: yyyy.MM.dd + * Description: Custom pattern for date only including year, month, and day with period as separator. + */ + public static final String CUSTOM_DATE_YEAR_FIRST_DOT_PATTERN = "yyyy.MM.dd"; + + /** + * Format: dd/MM/yyyy + * Description: Custom pattern for date only including day, month, and year with slashes as separator. + */ + public static final String CUSTOM_DATE_DD_MM_YYYY_SLASH_PATTERN = "dd/MM/yyyy"; + + /** + * Format: yyyy/MM/dd + * Description: Custom pattern for date only including year, month, and day with slashes as separator. + */ + public static final String CUSTOM_DATE_YYYY_MM_DD_SLASH_PATTERN = "yyyy/MM/dd"; + + /** + * Format: yyyyMMddHHmmss.SSSX + * Description: Compact pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone. + */ + public static final String CUSTOM_DATE_TIME_COMPACT_WITH_MILLISECONDS_PATTERN = "yyyyMMddHHmmss.SSSX"; +} \ No newline at end of file