Skip to content

Commit db60401

Browse files
committed
✨ feat: add constant time format #3
1 parent 8b19370 commit db60401

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

plugin/src/main/groovy/org/unify4j/common/Time4j.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,47 @@ public static String since(Date source, Date target) {
13441344
}
13451345
}
13461346

1347+
/**
1348+
* Formats the given epoch time into a human-readable "time ago" format.
1349+
* Examples:
1350+
* - "Just now" for times within a minute.
1351+
* - "X seconds ago," "X minutes ago," "X hours ago," "X days ago."
1352+
*
1353+
* @param epochMilli The epoch time in milliseconds.
1354+
* @return A string representing the time difference.
1355+
*/
1356+
public static String since(long epochMilli) {
1357+
Instant now = Instant.now();
1358+
Instant instant = Instant.ofEpochMilli(epochMilli);
1359+
LocalDate _now = LocalDate.now();
1360+
LocalDate local = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).toLocalDate();
1361+
// Calculate duration for seconds, minutes, hours, days
1362+
Duration duration = Duration.between(instant, now);
1363+
long seconds = duration.getSeconds();
1364+
long minutes = duration.toMinutes();
1365+
long hours = duration.toHours();
1366+
long days = duration.toDays();
1367+
1368+
// Calculate period for months and years
1369+
Period period = Period.between(local, _now);
1370+
int months = period.getMonths();
1371+
int years = period.getYears();
1372+
1373+
if (seconds < 60) {
1374+
return seconds <= 1 ? "Just now" : seconds + " seconds ago";
1375+
} else if (minutes < 60) {
1376+
return minutes == 1 ? "1 minute ago" : minutes + " minutes ago";
1377+
} else if (hours < 24) {
1378+
return hours == 1 ? "1 hour ago" : hours + " hours ago";
1379+
} else if (days < 30) {
1380+
return days == 1 ? "1 day ago" : days + " days ago";
1381+
} else if (months < 12) {
1382+
return months == 1 ? "1 month ago" : months + " months ago";
1383+
} else {
1384+
return years == 1 ? "1 year ago" : years + " years ago";
1385+
}
1386+
}
1387+
13471388
/**
13481389
* Provides the difference in milliseconds between the given dates.
13491390
*

plugin/src/main/groovy/org/unify4j/text/TimeFormatText.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,46 @@ public class TimeFormatText {
182182
* Description: Pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone offset.
183183
*/
184184
public static final String ISO_DATE_TIME_WITH_TIMEZONE_OFFSET = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
185-
}
186185

186+
/**
187+
* Format: dd.MM.yyyy
188+
* Description: Custom pattern for date only including day, month, and year with period as separator.
189+
*/
190+
public static final String CUSTOM_DATE_DOT_PATTERN = "dd.MM.yyyy";
191+
192+
/**
193+
* Format: HH.mm
194+
* Description: Custom short pattern for time only including hour and minutes with period as separator.
195+
*/
196+
public static final String CUSTOM_TIME_HH_MM_DOT_PATTERN = "HH.mm";
197+
198+
/**
199+
* Format: HH.mm.ss
200+
* Description: Custom short pattern for time including hour, minutes, and seconds with period as separator.
201+
*/
202+
public static final String CUSTOM_TIME_HH_MM_SS_DOT_PATTERN = "HH.mm.ss";
203+
204+
/**
205+
* Format: yyyy.MM.dd
206+
* Description: Custom pattern for date only including year, month, and day with period as separator.
207+
*/
208+
public static final String CUSTOM_DATE_YEAR_FIRST_DOT_PATTERN = "yyyy.MM.dd";
209+
210+
/**
211+
* Format: dd/MM/yyyy
212+
* Description: Custom pattern for date only including day, month, and year with slashes as separator.
213+
*/
214+
public static final String CUSTOM_DATE_DD_MM_YYYY_SLASH_PATTERN = "dd/MM/yyyy";
215+
216+
/**
217+
* Format: yyyy/MM/dd
218+
* Description: Custom pattern for date only including year, month, and day with slashes as separator.
219+
*/
220+
public static final String CUSTOM_DATE_YYYY_MM_DD_SLASH_PATTERN = "yyyy/MM/dd";
221+
222+
/**
223+
* Format: yyyyMMddHHmmss.SSSX
224+
* Description: Compact pattern for date and time including year, month, day, hour, minutes, seconds, milliseconds, and timezone.
225+
*/
226+
public static final String CUSTOM_DATE_TIME_COMPACT_WITH_MILLISECONDS_PATTERN = "yyyyMMddHHmmss.SSSX";
227+
}

0 commit comments

Comments
 (0)