获取时间差几小时几分钟前 (类似于新浪微博 发表于几小时几分钟前)

2021年9月6日 2点热度 0条评论 来源: leoxl8

 /**2.
     * 获取时间差xx小时xx分钟前(类似于新浪微博 的某条微博发表于几小时几分钟前)
     *
     * @param currentTime
     *            当前时间 2012-9-10 11:50:18
     * @param oldTime
     *            老时间 2012-9-10 10:20:08
     * @return 描述
     * @author xl@yang
     */
    public static String getTimeGap(String currentTime, String oldTime)
    {
        String hDes = "";
        String mDes = "";
        String[] newtime = currentTime.split(":");
        int newH = Integer.parseInt(newtime[0]);
        int newM = Integer.parseInt(newtime[1]);

        String[] oldtime = oldTime.split(":");
        int oldH = Integer.parseInt(oldtime[0]);
        int oldM = Integer.parseInt(oldtime[1]);

        int h = newH - oldH;
        int m = newM - oldM;
        int i = 0;
        int k = 0;
        if (0 < h)
        {
            if (0 < m)
            {
                hDes = h + "小时";
                mDes = m + "分钟";
            }
            else if (0 > m)
            {
                i = 60 - oldM + newM;
                mDes = i + "分钟";
                if (1 < h)
                {
                    k = h - 1;
                    hDes = k + "小时";
                }
            }
            else if (0 == m)
            {
                hDes = h + "小时";
            }
        }
        else if (0 < m)
        {
            mDes = m + "分钟";
        }
        return hDes + mDes + "前";
    }

 

    原文作者:leoxl8
    原文地址: https://blog.csdn.net/yangliang8/article/details/7962724
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。