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");
            }
        };

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

  • 相关阅读:
    HDU3371--Connect the Cities
    HDU1232--畅通工程
    HDU1102--Constructing Roads
    HDU1856--More is better
    HDU1325--Is It A Tree?
    HDU1272--小希的迷宫
    HDU1213--How Many Tables
    lnmp 实现owncloud
    lemp 编译安装 不完整版
    dns 视图
  • 原文地址:https://www.cnblogs.com/lee0oo0/p/3194703.html
Copyright © 2011-2022 走看看