Android项目中时间差的代码写法

该博客详细介绍了如何在Android项目中计算两个时间点之间的差值,以便以"刚刚"、"几分钟前"、"几小时前"等形式展示时间。提供了一个名为getTimeInterval的公共方法,该方法接受一个特定格式的日期字符串,通过与当前时间对比,返回对应的时间间隔描述。

一,在android中时间的形式包括:

1、时间戳 (1441355176150)

距1970年1月1日的毫秒数,一般作为对数据唯一性的一种判断依据,避免了重复修改数据所带来的错误。

获取方法(单位毫秒):

Long time = Calendar.getInstance().getTimeInMillis();

Long time = new Date().getTime();

Long time = System.currentTimeMillis();

2、Date时间格式



3、时间的字符串格式(20150903112723

直接将时间的各个组成部分拼成String类型组成。例如:2017年03月03日11时27分23秒 ,2017-03-03 11:27:23都是时间的字符串形式。


二,项目中的时间格式以及时间差(几年前,几个月前,几天前,几小时前,刚刚)

 
public static String getTimeInterval(String inputTime) {

    // inputTime传入的时间格式必须类似于“yyyy-MM-dd HH:mm:ss”这样的格式

    if (inputTime.length() != 19) {
        return inputTime;
    }

    String result = null;

    try {

        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //  ParsePosition 是指从哪个位置开始索引

        ParsePosition pos = new ParsePosition(0);

        Date d1 = sd.parse(inputTime, pos);

         // 用现在距离1970年的时间间隔new

        // Date().getTime()减去以前的时间距离1970年的时间间隔d1.getTime()得出的就是以前的时间与现在时间的时间间隔

        long time = new Date().getTime() - d1.getTime();// 得出的时间间隔是毫秒

        if (time / 1000 < 60) {

            // 如果时间间隔小于等于60秒则显示刚刚
            result = "刚刚";

        } else if (time / 60000 < 60) {

            // 如果时间间隔小于60分钟则显示多少分钟前

            result = time / 60000 + "分钟前";

        } else if (time / 3600000 < 24) {

            // 如果时间间隔小于24小时则显示多少小时前

            result = time / 3600000 + "小时前";

        } else if (time / 86400000 < 2) {

            // 如果时间间隔小于2天则显示昨天(3600000*24
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

            result = sdf.format(d1.getTime());

            result = "昨天" +result;

        } else if (time / 86400000 < 3) {

            // 如果时间间隔小于3天则显示前天

            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

            result = sdf.format(d1.getTime());

            result = "前天" +result;

        } else if (time / 86400000 < 30) {

            // 如果时间间隔小于30天则显示多少天前

            result = time / 86400000 + "天前";

        } else if (time / 86400000 < 60) {

            result = "1个月前";

        } else if (time / 86400000 < 90) {

            result = "2个月前";

        } else if (time / 86400000 < 120) {

            result = "3个月前";

        } else if (time / 86400000 < 150) {

            result = "4个月前";

        } else if (time / 86400000 < 180) {

            result = "5个月前";

        } else if (time / 86400000 < 210) {

            result = "6个月前";

        } else if (time / 86400000 < 240) {

            result = "7个月前";

        } else if (time / 86400000 < 270) {

            result = "8个月前";

        } else if (time / 86400000 < 300) {

            result = "9个月前";

        } else if (time / 86400000 < 330) {

            result = "10个月前";

        } else if (time / 86400000 < 360) {

            result = "11个月前";

        } else if (time / 86400000 < 720) {

            result = "1年前";

        } else if (time / 86400000 < 1080) {

            result = "2年前";

        } else {
            
            // 大于2年,显示年月日时间

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            result = sdf.format(d1.getTime());

        }

    } catch (Exception e) {

        return inputTime;

    }

    return result;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值