From cde72facfeae61f70667c1d436279cef3553d72e Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 19 Jun 2017 11:35:48 +0800 Subject: [PATCH] Bugfix: relative date format calculate error --- .../portal/util/RelativeDateFormat.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RelativeDateFormat.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RelativeDateFormat.java index 71f1a4ac9..2f899be19 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RelativeDateFormat.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/util/RelativeDateFormat.java @@ -2,6 +2,7 @@ package com.ctrip.framework.apollo.portal.util; import org.apache.commons.lang.time.FastDateFormat; +import java.util.Calendar; import java.util.Date; @@ -35,9 +36,15 @@ public class RelativeDateFormat { long hours = toHours(delta); return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO; } - if (delta < 48L * ONE_HOUR) { + + Date lastDayBeginTime = getDateOffset(-1); + if (date.after(lastDayBeginTime)) { return "昨天"; } + Date lastTwoDaysBeginTime = getDateOffset(-2); + if (date.after(lastTwoDaysBeginTime)) { + return "前天"; + } if (delta < 30L * ONE_DAY) { long days = toDays(delta); return (days <= 0 ? 1 : days) + ONE_DAY_AGO; @@ -71,4 +78,22 @@ public class RelativeDateFormat { 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()); + } + } -- GitLab