Skip to content

Commit

Permalink
Merge pull request apolloconfig#642 from lepdou/bugfix_relativedate
Browse files Browse the repository at this point in the history
Bugfix: relative date format calculate error
  • Loading branch information
nobodyiam authored Jun 19, 2017
2 parents 5bcb409 + cde72fa commit 8db5e26
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

import java.util.Calendar;
import java.util.Date;


Expand Down Expand Up @@ -35,9 +36,15 @@ public static String format(Date date) {
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;
Expand Down Expand Up @@ -71,4 +78,22 @@ 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 8db5e26

Please sign in to comment.