首页 > 其他 > 详细

如何将时间友好展示

时间:2015-11-03 20:48:29      阅读:251      评论:0      收藏:0      [点我收藏+]

 

java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date date = formatter.parse(“2015-10-15 17:10:58”);
                holder.mtime.setText(StringUtils.checkNull(DateUtil.friendlyTime(date)));
            }
            catch (ParseException e) {             
                e.printStackTrace();
            }

 

/**
     * 以友好的方式显示时间
     *
     * @param time
     * @return
     */
    public static String friendlyTime(Date time) {
        // 获取time距离当前的秒数
        int ct = (int) ((System.currentTimeMillis() - time.getTime()) / 1000);
        if (checkDateTime(time).equals("0")) {
            if (ct <= 0) {
                return "1分钟前";
            }

            if (ct > 0 && ct < 60) {
                return "1分钟前";
            }

            if (ct >= 60 && ct < 3600) {
                return Math.max(ct / 60, 1) + "分钟前";
            }
            if (ct >= 3600 && ct < 86400) {
                SimpleDateFormat df = new SimpleDateFormat("HH:mm");
                String hour = df.format(time).substring(0, 2);
                int hours = Integer.parseInt(hour);
                if (hours > 12) {
                    return "下午" + df.format(time);
                }
                else {
                    return "上午" + df.format(time);
                }
            }
        }
        else if (checkDateTime(time).equals("1")) {

            return "昨天";
        }
        else {      
            SimpleDateFormat mh = new SimpleDateFormat("yyyy-MM-dd");
            String format = mh.format(time.getTime());
            return format;

        }
        return ct / 31104000 + "年前";

    }

如何将时间友好展示

原文:http://www.cnblogs.com/xidada/p/4934213.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!