Skip to content

Commit

Permalink
optimization omits unnecessary time conversion (#4118)
Browse files Browse the repository at this point in the history
optimize the step of omitting time conversion milliseconds and then turning back time

Co-authored-by: Jason Song <[email protected]>
  • Loading branch information
xuxiawei and nobodyiam authored Nov 27, 2021
1 parent 4a20206 commit 75f9950
Showing 1 changed file with 33 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

import org.apache.commons.lang.time.FastDateFormat;

import java.util.Calendar;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


public class RelativeDateFormat {
private static final FastDateFormat TIMESTAMP_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");
private static final long ONE_MINUTE = 60000L;
private static final long ONE_HOUR = 3600000L;
private static final long ONE_DAY = 86400000L;

private static final String ONE_SECOND_AGO = " seconds ago";
private static final String ONE_MINUTE_AGO = " minutes ago";
Expand All @@ -35,80 +35,34 @@ public class RelativeDateFormat {
private static final String ONE_MONTH_AGO = " months ago";

public static String format(Date date) {
if (date.after(new Date())) {
return "now";
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
Duration duration = Duration.between(localDateTime, LocalDateTime.now());
if (duration.toMillis() <= 0L) {
return "now";
}
if (duration.getSeconds() <= 60L) {
return (duration.getSeconds() <= 0 ? 1 : duration.getSeconds()) + ONE_SECOND_AGO;
}
if (duration.toMinutes() < 45L) {
return (duration.toMinutes() <= 0 ? 1 : duration.toMinutes()) + ONE_MINUTE_AGO;
}
if (duration.toHours() < 24L) {
return (duration.toHours() <= 0 ? 1 : duration.toHours()) + ONE_HOUR_AGO;
}
if (localDateTime.isAfter(LocalDateTime.now().minusDays(1))) {
return "yesterday";
}
if (localDateTime.isAfter(LocalDateTime.now().minusDays(2))) {
return "the day before yesterday";
}
if (duration.toDays() < 30L) {
return (duration.toDays() <= 0 ? 1 : duration.toDays()) + ONE_DAY_AGO;
}
if (duration.toDays() / 30 <= 3L) {
return duration.toDays() / 30 + ONE_MONTH_AGO;
}
return TIMESTAMP_FORMAT.format(date);
}

long delta = System.currentTimeMillis() - date.getTime();
if (delta < ONE_MINUTE) {
long seconds = toSeconds(delta);
return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
}
if (delta < 45L * ONE_MINUTE) {
long minutes = toMinutes(delta);
return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
}
if (delta < 24L * ONE_HOUR) {
long hours = toHours(delta);
return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
}

Date lastDayBeginTime = getDateOffset(-1);
if (date.after(lastDayBeginTime)) {
return "yesterday";
}
Date lastTwoDaysBeginTime = getDateOffset(-2);
if (date.after(lastTwoDaysBeginTime)) {
return "the day before yesterday";
}
if (delta < 30L * ONE_DAY) {
long days = toDays(delta);
return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
}

long months = toMonths(delta);
if (months <= 3) {
return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
}
return TIMESTAMP_FORMAT.format(date);
}

private static long toSeconds(long date) {
return date / 1000L;
}

private static long toMinutes(long date) {
return toSeconds(date) / 60L;
}

private static long toHours(long date) {
return toMinutes(date) / 60L;
}

private static long toDays(long date) {
return toHours(date) / 24L;
}

private static long toMonths(long date) {
return toDays(date) / 30L;
}

public static Date getDateOffset(int offset) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, offset);

return getDayBeginTime(calendar.getTime());
}

private static Date getDayBeginTime(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Date(calendar.getTime().getTime());
}

}

0 comments on commit 75f9950

Please sign in to comment.