一、业务场景
论坛消息等需要展示接收时间,如“几秒前”、“几分钟前”,“几天前”等,如图所示:
二、代码实现
新建class:RelativeDateHandler.java
package com.test;
import java.util.Date;
public class RelativeDateHandler {
private static final long ONE_MINUTE = 60000L;
private static final long ONE_HOUR = 3600000L;
private static final long ONE_DAY = 86400000L;
private static final long ONE_WEEK = 604800000L;
private static final String ONE_SECOND_AGO = "秒前";
private static final String ONE_MINUTE_AGO = "分钟前";
private static final String ONE_HOUR_AGO = "小时前";
private static final String ONE_DAY_AGO = "天前";
private static final String ONE_MONTH_AGO