zoukankan      html  css  js  c++  java
  • Android之计算两个时间的相差

    参数:   sdate = 2013-07-16 16:14:47

    /**
         * 以友好的方式显示时间
         * @param sdate
         * @return
         */
        public static String friendly_time(String sdate) {
            Date time = toDate(sdate);
            if(time == null) {
                return "Unknown";
            }
            String ftime = "";
            Calendar cal = Calendar.getInstance();
            
            //判断是否是同一天
            String curDate = dateFormater2.get().format(cal.getTime());
            String paramDate = dateFormater2.get().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 <= 10){ 
                ftime = days+"天前";            
            }
            else if(days > 10){            
                ftime = dateFormater2.get().format(time);
            }
            return ftime;
        }
    
    /**
         * 将字符串转位日期类型
         * @param sdate
         * @return
         */
        public static Date toDate(String sdate) {
            try {
                return dateFormater.get().parse(sdate);
            } catch (ParseException e) {
                return null;
            }
        }
    
    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }
        };
    
    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
            @Override
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat("yyyy-MM-dd");
            }
        };

    ---恢复内容结束---

  • 相关阅读:
    Matlab之rand(), randn(), randi()函数的使用方法
    matlab给图片插入说明文字
    matlab之find()函数
    excel根据数据源变化的动态图表
    高斯坐标
    (转)Mysql技术内幕InnoDB存储引擎-表&索引算法和锁
    (转)MySQL 插入数据时,中文乱码问题的解决
    (转)防止人为误操作MySQL数据库技巧一例
    (转)mysql explain详解
    (转)一个MySQL 5.7 分区表性能下降的案例分析
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3194703.html
Copyright © 2011-2022 走看看