zoukankan      html  css  js  c++  java
  • 【Java】PrettyTime

    package test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * Created by luan on 2015/9/15.
     */
    public class PrettyTime{
    
        private final static long minute = 60 * 1000;// 1分钟
        private final static long hour = 60 * minute;// 1小时
        private final static long day = 24 * hour;// 1天
        private final static long month = 31 * day;// 月
        private final static long year = 12 * month;// 年
    
        public static void main(String[] args) throws ParseException {
    
            String m = "2015-09-15 09:35:01";
    
            System.out.printf(getTimeFormatText(m));
    
        }
    
        /**
         * 返回文字描述的日期
         * @param date
         * @return
         */
        public static String getTimeFormatText(String date) {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            Date d;
            try {
                d = f.parse(date);
            } catch (ParseException e) {
                return date;
            }
    
            return getTimeFormatText(d);
        }
    
        /**
         * 返回文字描述的日期
         *
         * @param date
         * @return
         */
        public static String getTimeFormatText(Date date) {
            if (date == null) {
                return null;
            }
            long diff = new Date().getTime() - date.getTime();
            long r = 0;
            if (diff > year) {
                r = (diff / year);
                return r + "年前";
            }
            if (diff > month) {
                r = (diff / month);
                return r + "个月前";
            }
            if (diff > day) {
                r = (diff / day);
                return r + "天前";
            }
            if (diff > hour) {
                r = (diff / hour);
                return r + "个小时前";
            }
            if (diff > minute) {
                r = (diff / minute);
                return r + "分钟前";
            }
            return "刚刚";
        }
    
    }
    

      

  • 相关阅读:
    存储管理:
    小软件推荐:
    python学习之序列化
    python学习之串口编程
    python学习之常用模块
    Linux调试工具strace和gdb常用命令小结
    adjtimex和时钟的几个概念tick,freq,ppm,jiffies
    koji
    top -Hp pid 显示所有的线程
    Neighbour table overflow---- arp表溢出
  • 原文地址:https://www.cnblogs.com/lhp2012/p/4809349.html
Copyright © 2011-2022 走看看