获取时间差(类似微信朋友圈消息的时间显示)

1、上代码

package com.v512.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeCalc {
    public String  value(String newTime,String oldTime) throws ParseException {
        // TODO Auto-generated method stub
        String dateline = null;
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = sdf1.parse(oldTime);
        Date d2 = sdf1.parse(newTime);
        dateline = String.valueOf(daysBetween(d1, d2))+"天前"; //获取两个时间的天数差
        if(daysBetween(d1, d2)==0){     //判定是否是同一天,不是一天的话进行下一步判断
            //判定一天内的间隔时间
            dateline = getTimeGap(oldTime.substring(oldTime.length()-8),newTime.substring(newTime.length()-8));
        }else if(daysBetween(d1, d2)==1){  //相差一天,就返回昨天
            dateline = "昨天";
        }
        return dateline;   //相差两天以上
    }

    public static String getTimeGap(String oldTime,String currentTime) {

        String hDes = "";
        String mDes = "";
        String[] newtime = currentTime.split(":");
        int newH = Integer.parseInt(newtime[0]);//记录小时
        int newM = Integer.parseInt(newtime[1]);//记录分钟
        int newS = Integer.parseInt(newtime[2]);//记录秒钟

        String[] oldtime = oldTime.split(":");
        int oldH = Integer.parseInt(oldtime[0]);//功能如上
        int oldM = Integer.parseInt(oldtime[1]);
        int oldS = Integer.parseInt(oldtime[2]);

        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 + "分钟";
        }
        if((hDes + mDes).equals("")){
                return newS-oldS+"秒前";    //小时为0,则返回秒
        }
        if(Integer.valueOf(hDes.substring(0, hDes.indexOf("小时")))>1){   //如果小时不为0,则只返回小时
            return hDes.substring(0, hDes.indexOf("小时"))+"小时前";
        }
        return hDes + mDes + "前";   
    }

    /**
     * 计算两个日期之间相差的天数
     * 
     * @param smdate
     *            较小的时间
     * @param bdate
     *            较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 字符串的日期格式的计算
     */
    public static int daysBetween(String smdate, String bdate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(smdate));
        long time1 = cal.getTimeInMillis();
        cal.setTime(sdf.parse(bdate));
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }
}

2、使用
入口为:public String value(String newTime,String oldTime) throws ParseException 这个方法,需要传进去两个String类型的时间参数,格式为:yyyy-MM-dd HH:mm:ss

3、方法介绍、(参考了网上的一些代码,不是完全自己写的,所以代码质量可能不大好)
public String value(String newTime,String oldTime)这个方法返回的就是我们所求的时间:

4、缺点、天数相差过长的不能按月份显示

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值