Class RelativeTimeFormatter
java.lang.Object
cloud.opencode.base.date.formatter.RelativeTimeFormatter
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 TypeMethodDescriptionstatic StringFormats an Instant as a relative time string 将Instant格式化为相对时间字符串static StringFormats a temporal as a relative time string in English 将时间格式化为英文相对时间字符串static StringFormats a temporal as a relative time string relative to a reference time 相对于参考时间将时间格式化为相对时间字符串static StringformatChinese(Instant instant) Formats an Instant as a relative time string in Chinese 将Instant格式化为中文相对时间字符串static StringformatChinese(Temporal temporal) Formats a temporal as a relative time string in Chinese 将时间格式化为中文相对时间字符串static StringformatChinese(Temporal temporal, Temporal reference) Formats a temporal as a relative time string in Chinese relative to reference 相对于参考时间将时间格式化为中文相对时间字符串static StringformatCompact(Temporal temporal) Formats as a compact relative time string (e.g., "5m", "3h", "2d") 格式化为紧凑的相对时间字符串(如"5m"、"3h"、"2d")static StringformatDuration(Duration duration) Formats a Duration as a relative time string 将Duration格式化为相对时间字符串static StringformatDurationChinese(Duration duration) Formats a Duration as a relative time string in Chinese 将Duration格式化为中文相对时间字符串static StringformatSmart(LocalDateTime dateTime) Formats using smart relative time with day-specific terms 使用智能相对时间格式化,包含日期特定术语static StringformatSmartChinese(LocalDateTime dateTime) Formats using smart relative time with day-specific terms in Chinese 使用智能相对时间格式化,包含中文日期特定术语
-
Method Details
-
format
-
format
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
-
formatDuration
-
formatChinese
-
formatChinese
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
-
formatDurationChinese
-
formatSmart
Formats using smart relative time with day-specific terms 使用智能相对时间格式化,包含日期特定术语- Parameters:
dateTime- the date-time to format | 要格式化的日期时间- Returns:
- the smart relative time string | 智能相对时间字符串
-
formatSmartChinese
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
-