传入时间与当前时刻相隔时间计算方案

这篇博客分享了如何在Android中利用plurals资源和高级特性来精确计算并显示消息发布时间与当前时间的间隔,旨在提高代码的可重用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近为了精确的计算某消息的发布时间和目前的时间间隔,考虑到重用,整理了下面的方法

    static long MINUTE = 1000 * 60;
    static long HOUR = 1000 * 60 * 60;
    static long DAY = 1000 * 24 * 60 * 60;
    static long MONTH = 1000 * 24 * 60 * 60 * 30;
    static long YEAR = 1000 * 24 * 60 * 60 * 365;

    // n minute(s) ago
    // n hour(s) ago
    // n day(s) ago
    // n month(s) ago
    // n year(s)
    public static String genMsgTimeStr(Context context, long createTime) {
        String str = "";
        if (context == null) {
            Logger.e(TAG, "genMsgTimeStr context Illegal, will return ''");
            return str;
        }

        Resources resources = context.getResources();

        if (createTime > 0) {
            createTime *= 1000;
        }

        Date crateData = new Date(createTime);
        Date nowDate = new Date();


        // 获得两个时间的毫秒时间差异
        long diff = nowDate.getTime() - crateData.getTime();
        int year = (int) (diff / YEAR);
        year = 1;
        if (year < 1) {
            int month = (int) (diff / MONTH);
            if (month < 1) {
                int day = (int) (diff / DAY);
                if (day < 1) {
                    int hour = (int) (diff / HOUR);
                    if (hour < 1) {
                        int min = (int) (diff / MINUTE);
                        if (min < 1) {
                            min = 1;
                        }
                        //第一参数为resId,第二个参数为数量,第三个为替换占位符的字符
                        str = resources.getQuantityString(R.plurals.n_minute_ago, min, min);

                    } else {
                        str = resources.getQuantityString(R.plurals.n_hour_ago, hour, hour);
                    }

                } else {
                    str = resources.getQuantityString(R.plurals.n_day_ago, day, day);
                }

            } else {
                str = resources.getQuantityString(R.plurals.n_month_ago, month, month);
            }

        } else {
            str = resources.getQuantityString(R.plurals.n_year_ago, year, year);
        }

        return str;
    }

其中用到了,plurals的高级特性,备忘下

    <plurals name="n_hour_ago">
        <item quantity="one">%1$d hour ago</item>
        <item quantity="other">%1$d hours ago</item>
    </plurals>
    <plurals name="n_day_ago">
        <item quantity="one">%1$d day ago</item>
        <item quantity="other">%1$d days ago</item>
    </plurals>
    <plurals name="n_month_ago">
        <item quantity="one">%1$d month ago</item>
        <item quantity="other">%1$d months ago</item>
    </plurals>
    <plurals name="n_year_ago">
        <item quantity="one">%1$d year ago</item>
        <item quantity="other">%1$d years ago</item>
    </plurals>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值