Commit 8db5e264 by Jason Song Committed by GitHub

Merge pull request #642 from lepdou/bugfix_relativedate

Bugfix: relative date format calculate error
parents 5bcb4095 cde72fac
...@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.portal.util; ...@@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.portal.util;
import org.apache.commons.lang.time.FastDateFormat; import org.apache.commons.lang.time.FastDateFormat;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
...@@ -35,9 +36,15 @@ public class RelativeDateFormat { ...@@ -35,9 +36,15 @@ public class RelativeDateFormat {
long hours = toHours(delta); long hours = toHours(delta);
return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO; return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
} }
if (delta < 48L * ONE_HOUR) {
Date lastDayBeginTime = getDateOffset(-1);
if (date.after(lastDayBeginTime)) {
return "昨天"; return "昨天";
} }
Date lastTwoDaysBeginTime = getDateOffset(-2);
if (date.after(lastTwoDaysBeginTime)) {
return "前天";
}
if (delta < 30L * ONE_DAY) { if (delta < 30L * ONE_DAY) {
long days = toDays(delta); long days = toDays(delta);
return (days <= 0 ? 1 : days) + ONE_DAY_AGO; return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
...@@ -71,4 +78,22 @@ public class RelativeDateFormat { ...@@ -71,4 +78,22 @@ public class RelativeDateFormat {
return toDays(date) / 30L; 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());
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment