App通用时间规则

一直都有留意QQ,微信的时间记录,这些时间记录会随着时间改变而且改变。趁这几天有空,特意研究了它们的时间规则。

时间规则大体如下(根据各自公司需求会有所不同)
1分钟内发布的:刚刚
1小时内发布的:X分钟前
超过1小时,仍在当天:xx:xx
跨天,但少于24小时:昨天 xx:xx
跨天,超过24小时:xxxx-xx-xx xx:xx

跨天的定义:过了凌晨00:00则为跨天;
跨月的定义:过了对应月份最后一天23:59则为跨月;
跨年的定义:过了当前年份12月31日23:59则为跨年;

/**
 * 测试类
 * Created by Edward
 */
public class MyTestClass {

    public static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        //unix时间戳转为java时间戳
        long time = Long.parseLong("1483193797") * 1000;
        System.out.println("起始时间:" + sdf.format(time));
        //time的参数单位是毫秒
        String temp = new MyTestClass().timeRule(time);
        System.out.println(temp);
    }

    /**
     * @return
     */
    private long getCurrentTime() {
        //用于测试
        return Long.parseLong("1483299397") * 1000;
        //正式环境
//        return System.currentTimeMillis();
    }

    /**
     * @param time java时间戳(注意不是unix时间戳)
     * @return
     * @throws Exception
     */
    private String timeRule(long time) throws Exception {
        /**
         * 分钟
         */
        final int MINUTE = 60;
        /**
         * 小时
         */
        final int HOUR = 3600;
        /**
         * 24小时
         */
        final int TWENTY_FOUR_HOUR = 86400;

        long currentTime = getCurrentTime();
        System.out.println("当前时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(currentTime));
        //时间单位:秒
        long newTime = (currentTime - time) / 1000;
        if (newTime < 0) {
            throw new Exception(newTime + " timestamp exception!");
        } else {
            System.out.println("时间差(秒):" + newTime);
        }

        //如果小于1分钟
        if (newTime <= MINUTE) {
            return "刚刚";
        }

        //如果大于1分钟且小于1小时
        if (newTime > MINUTE && newTime <= HOUR) {
            return (newTime / MINUTE) + "分钟前";
        }

        //大于1小时且小于24小时
        if (newTime > HOUR && newTime <= TWENTY_FOUR_HOUR) {
            return isIntraday(getCurrentTime(), time) ? getTimeStr("HH:mm", time) : "昨天" + getTimeStr("HH:mm", time);
        }

        //超过24小时,直接显示日期加时间
        if (newTime > TWENTY_FOUR_HOUR) {
            return getTimeStr("yyyy-MM-dd HH:mm", time);
        }

        return null;
    }

    /**
     * 判断currentTime和time是否同一天
     *
     * @param currentTime
     * @param time
     * @return true表示同一天, 否则为false
     */
    private boolean isIntraday(long currentTime, long time) {
        String curTimeStr = getTimeStr("yyyy-MM-dd", currentTime);
        String oldTimeStr = getTimeStr("yyyy-MM-dd", time);
        return curTimeStr.equals(oldTimeStr);
    }

    /**
     * 将时间戳转为指定的字符串格式
     *
     * @param format
     * @param time
     * @return
     */
    private String getTimeStr(String format, long time) {
        return new SimpleDateFormat(format, Locale.CHINA).format(time);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值