Class RelativeTimeFormatter

java.lang.Object
cloud.opencode.base.date.formatter.RelativeTimeFormatter

public final class RelativeTimeFormatter extends Object
Formats temporal objects as relative time strings (e.g., "3 hours ago", "in 2 days") 将时间对象格式化为相对时间字符串(如"3小时前"、"2天后")

This class provides human-friendly relative time formatting, similar to what you see in social media applications ("just now", "5 minutes ago", "yesterday").

此类提供人性化的相对时间格式化,类似于社交媒体应用中看到的 ("刚刚"、"5分钟前"、"昨天")。

Features | 主要功能:

  • Smart relative time formatting - 智能相对时间格式化
  • Support for past and future times - 支持过去和未来时间
  • Multiple language support (EN/CN) - 多语言支持(英文/中文)
  • Configurable thresholds - 可配置的阈值

Usage Examples | 使用示例:

LocalDateTime time = LocalDateTime.now().minusMinutes(5);
String relative = RelativeTimeFormatter.format(time);
// "5 minutes ago"

String relativeCn = RelativeTimeFormatter.formatChinese(time);
// "5分钟前"

// Future time
LocalDateTime future = LocalDateTime.now().plusDays(2);
String inFuture = RelativeTimeFormatter.format(future);
// "in 2 days"

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)

Performance | 性能特性:

  • Time complexity: O(1) - all format methods compare against a fixed set of time thresholds - 时间复杂度: O(1) - 所有 format 方法均与固定数量的时间阈值进行比较
  • Space complexity: O(1) - output string size is bounded by fixed message templates - 空间复杂度: O(1) - 输出字符串大小由固定消息模板限定
Since:
JDK 25, opencode-base-date V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    format(Instant instant)
    Formats an Instant as a relative time string 将Instant格式化为相对时间字符串
    static String
    format(Temporal temporal)
    Formats a temporal as a relative time string in English 将时间格式化为英文相对时间字符串
    static String
    format(Temporal temporal, Temporal reference)
    Formats a temporal as a relative time string relative to a reference time 相对于参考时间将时间格式化为相对时间字符串
    static String
    Formats an Instant as a relative time string in Chinese 将Instant格式化为中文相对时间字符串
    static String
    Formats a temporal as a relative time string in Chinese 将时间格式化为中文相对时间字符串
    static String
    formatChinese(Temporal temporal, Temporal reference)
    Formats a temporal as a relative time string in Chinese relative to reference 相对于参考时间将时间格式化为中文相对时间字符串
    static String
    Formats as a compact relative time string (e.g., "5m", "3h", "2d") 格式化为紧凑的相对时间字符串(如"5m"、"3h"、"2d")
    static String
    Formats a Duration as a relative time string 将Duration格式化为相对时间字符串
    static String
    Formats a Duration as a relative time string in Chinese 将Duration格式化为中文相对时间字符串
    static String
    Formats using smart relative time with day-specific terms 使用智能相对时间格式化,包含日期特定术语
    static String
    Formats using smart relative time with day-specific terms in Chinese 使用智能相对时间格式化,包含中文日期特定术语

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • format

      public static String format(Temporal temporal)
      Formats a temporal as a relative time string in English 将时间格式化为英文相对时间字符串
      Parameters:
      temporal - the temporal to format | 要格式化的时间
      Returns:
      the relative time string | 相对时间字符串
    • format

      public static String format(Temporal temporal, Temporal reference)
      Formats a temporal as a relative time string relative to a reference time 相对于参考时间将时间格式化为相对时间字符串
      Parameters:
      temporal - the temporal to format | 要格式化的时间
      reference - the reference time | 参考时间
      Returns:
      the relative time string | 相对时间字符串
    • format

      public static String format(Instant instant)
      Formats an Instant as a relative time string 将Instant格式化为相对时间字符串
      Parameters:
      instant - the instant to format | 要格式化的Instant
      Returns:
      the relative time string | 相对时间字符串
    • formatDuration

      public static String formatDuration(Duration duration)
      Formats a Duration as a relative time string 将Duration格式化为相对时间字符串
      Parameters:
      duration - the duration to format | 要格式化的Duration
      Returns:
      the relative time string | 相对时间字符串
    • formatChinese

      public static String formatChinese(Temporal temporal)
      Formats a temporal as a relative time string in Chinese 将时间格式化为中文相对时间字符串
      Parameters:
      temporal - the temporal to format | 要格式化的时间
      Returns:
      the relative time string in Chinese | 中文相对时间字符串
    • formatChinese

      public static String formatChinese(Temporal temporal, Temporal reference)
      Formats a temporal as a relative time string in Chinese relative to reference 相对于参考时间将时间格式化为中文相对时间字符串
      Parameters:
      temporal - the temporal to format | 要格式化的时间
      reference - the reference time | 参考时间
      Returns:
      the relative time string in Chinese | 中文相对时间字符串
    • formatChinese

      public static String formatChinese(Instant instant)
      Formats an Instant as a relative time string in Chinese 将Instant格式化为中文相对时间字符串
      Parameters:
      instant - the instant to format | 要格式化的Instant
      Returns:
      the relative time string in Chinese | 中文相对时间字符串
    • formatDurationChinese

      public static String formatDurationChinese(Duration duration)
      Formats a Duration as a relative time string in Chinese 将Duration格式化为中文相对时间字符串
      Parameters:
      duration - the duration to format | 要格式化的Duration
      Returns:
      the relative time string in Chinese | 中文相对时间字符串
    • formatSmart

      public static String formatSmart(LocalDateTime dateTime)
      Formats using smart relative time with day-specific terms 使用智能相对时间格式化,包含日期特定术语
      Parameters:
      dateTime - the date-time to format | 要格式化的日期时间
      Returns:
      the smart relative time string | 智能相对时间字符串
    • formatSmartChinese

      public static String formatSmartChinese(LocalDateTime dateTime)
      Formats using smart relative time with day-specific terms in Chinese 使用智能相对时间格式化,包含中文日期特定术语
      Parameters:
      dateTime - the date-time to format | 要格式化的日期时间
      Returns:
      the smart relative time string in Chinese | 中文智能相对时间字符串
    • formatCompact

      public static String formatCompact(Temporal temporal)
      Formats as a compact relative time string (e.g., "5m", "3h", "2d") 格式化为紧凑的相对时间字符串(如"5m"、"3h"、"2d")
      Parameters:
      temporal - the temporal to format | 要格式化的时间
      Returns:
      the compact relative time string | 紧凑相对时间字符串