Class DateUtils


  • public class DateUtils
    extends Object
    Convert ISO timestamps to human-readable relative times
    • Constructor Detail

      • DateUtils

        public DateUtils()
    • Method Detail

      • toDateAndTimeAgo

        public static String toDateAndTimeAgo​(String isoFormattedDate)
        Parameters:
        isoFormattedDate - ISO timestamp "2021-12-31T23:59:59.123456789+18:00"
        Returns:
        Combine original timestamp at local time zone truncated to seconds and human-readable relative time: "2021-12-31T07:59:59+02:00 (1 year 2 months ago)"
        See Also:
        toTimeAgo(String)
      • toTimeAgo

        public static String toTimeAgo​(String isoFormattedDate)
        Parameters:
        isoFormattedDate - ISO timestamp "2021-12-31T23:59:59.123456789+18:00"
        Returns:
        human-readable relative time: "1 year 2 months ago"
        See Also:
        toTimeAgo(long)
      • toTimeAgo

        public static String toTimeAgo​(long epochMillis)

        Thresholds are 60 seconds, 50 minutes, 20 hours, 3 weeks, 10 months.
        Everything else is rounded (1.5 becomes 2).
        Surplus months except 1 are not rounded (1 year 2 months ago).
        1 month are 30.42 days.

        • <= 89 seconds: a minute ago
        • <= 50 minutes: 50 minutes ago
        • <= 89 minutes: an hour ago
        • <= 20 hours: 20 hours ago
        • <= 35 hours: yesterday
        • <= 5 days: 5 days ago
        • <= 10 days: a week ago
        • <= 24 days: 3 weeks ago
        • <= 45 days: a month ago
        • <= 10 months: 10 months ago
        • <= 13 months: a year ago
        • == 14 months: 1 year 2 months ago
        • == 23 months: 1 year 11 months ago
          0 sec = a minute ago
         89 sec = a minute ago
         90 sec = 2 minutes ago
         50 min = 50 minutes ago
         51 min = an hour ago
         89 min = an hour ago
         90 min = 2 hours ago
         20 hrs = 20 hours ago
         21 hrs = yesterday
         35 hrs = yesterday
         36 hrs = 2 days ago
          5 day = 5 days ago
          6 day = a week ago
         10 day = a week ago
         11 day = 2 weeks ago
         17 day = 2 weeks ago
         18 day = 3 weeks ago
         24 day = 3 weeks ago
         25 day = a month ago
         45 day = a month ago
         46 day = 2 months ago
         10 mon = 10 months ago
         11 mon = a year ago
         13 mon = a year ago
         14 mon = 1 year 2 months ago
         23 mon = 1 year 11 months ago
         25 mon = 2 years ago
         26 mon = 2 years 2 months ago
         

        Parameters:
        epochMillis - Unix time in milliseconds (since 1970-01-01)
        Returns:
        human readable relative time: "1 year 2 months ago"