朋友圈时间工具类

 public static String ChangeTime(Date time) {

        SimpleDateFormat dateFormat = new SimpleDateFormat();
        String ftime = "";
        Calendar cal = Calendar.getInstance();        // 判断是否是同一天
        String curDate = dateFormat.format(cal.getTime());
        String paramDate = dateFormat.format(time);

        if (curDate.equals(paramDate)) {

            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);

            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";

            else
                ftime = hour + "小时前";

            return ftime;
        }
        long lt = time.getTime() / 86400000;

        long ct = cal.getTimeInMillis() / 86400000;

        int days = (int) (ct - lt);

        if (days == 0) {

            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);

            if (hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天 ";
        } else if (days > 2 && days < 31) {
            ftime = days + "天前";
        } else if (days >= 31 && days <= 2 * 31) {
            ftime = "一个月前";
        } else if (days > 2 * 31 && days <= 3 * 31) {
            ftime = "2个月前";
        } else if (days > 3 * 31 && days <= 4 * 31) {
            ftime = "3个月前";
        } else {
            ftime = dateFormat.format(time);
        }
        return ftime;
    }

    public static String friendly_time(String sdate) {
        Date time = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat();
//        if (TimeZoneUtil.isInEasternEightZones())
//            time = toDate(sdate);
//        else
//            time = TimeZoneUtil.transformTime(toDate(sdate),
//                    TimeZone.getTimeZone("GMT+08"), TimeZone.getDefault());
//
//        if (time == null) {
//
//            return "Unknown";
//        }
        time = new Date(Long.valueOf(sdate));
        String ftime = "";
        Calendar cal = Calendar.getInstance();        // 判断是否是同一天
        String curDate = dateFormat.format(cal.getTime());
        String paramDate = dateFormat.format(time);

        if (curDate.equals(paramDate)) {

            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);

            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";

            return ftime;
        }
        long lt = time.getTime() / 86400000;

        long ct = cal.getTimeInMillis() / 86400000;

        int days = (int) (ct - lt);

        if (days == 0) {

            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);

            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天 ";
        } else if (days > 2 && days < 31) {
            ftime = days + "天前";
        } else if (days >= 31 && days <= 2 * 31) {
            ftime = "一个月前";
        } else if (days > 2 * 31 && days <= 3 * 31) {
            ftime = "2个月前";
        } else if (days > 3 * 31 && days <= 4 * 31) {
            ftime = "3个月前";
        } else {
            ftime = dateFormat.format(time);
        }
        return ftime;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String time)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return String.valueOf(ts);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值