Skip to content

Commit cde72fa

Browse files
committed
Bugfix: relative date format calculate error
1 parent 5bcb409 commit cde72fa

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RelativeDateFormat.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
import java.util.Calendar;
56
import java.util.Date;
67

78

@@ -35,9 +36,15 @@ public static String format(Date date) {
3536
long hours = toHours(delta);
3637
return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
3738
}
38-
if (delta < 48L * ONE_HOUR) {
39+
40+
Date lastDayBeginTime = getDateOffset(-1);
41+
if (date.after(lastDayBeginTime)) {
3942
return "昨天";
4043
}
44+
Date lastTwoDaysBeginTime = getDateOffset(-2);
45+
if (date.after(lastTwoDaysBeginTime)) {
46+
return "前天";
47+
}
4148
if (delta < 30L * ONE_DAY) {
4249
long days = toDays(delta);
4350
return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
@@ -71,4 +78,22 @@ private static long toMonths(long date) {
7178
return toDays(date) / 30L;
7279
}
7380

81+
public static Date getDateOffset(int offset) {
82+
Calendar calendar = Calendar.getInstance();
83+
calendar.setTime(new Date());
84+
calendar.add(calendar.DATE, offset);
85+
86+
return getDayBeginTime(calendar.getTime());
87+
}
88+
89+
private static Date getDayBeginTime(Date date) {
90+
Calendar calendar = Calendar.getInstance();
91+
calendar.setTime(date);
92+
calendar.set(Calendar.HOUR, 0);
93+
calendar.set(Calendar.MINUTE, 0);
94+
calendar.set(Calendar.SECOND, 0);
95+
calendar.set(Calendar.MILLISECOND, 0);
96+
return new Date(calendar.getTime().getTime());
97+
}
98+
7499
}

0 commit comments

Comments
 (0)