zoukankan      html  css  js  c++  java
  • 对比时间返回时间差

    //比对时间。
     public static String CompareTime(String stime){
     
     String returntime = null;
     
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date curDate = new Date(System.currentTimeMillis());//获取当前时间
         String str = format.format(curDate);
       
        Date d1 = null;
        Date d2 = null;
     
        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(str);
     
            //毫秒ms
            long diff = d2.getTime() - d1.getTime();
     
            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
     
            if(diffDays>0){//天
             returntime = String.valueOf(diffDays)+"天前";
            }else if(diffHours>0){//小时
             returntime = String.valueOf(diffHours)+"小时前";
            }else if(diffMinutes>0){//分钟
             returntime = String.valueOf(diffMinutes)+"分钟前";
            }else if(diffSeconds>0){
             returntime = String.valueOf(diffSeconds)+"秒前";
            }
     
        } catch (Exception e) {
            e.printStackTrace();
        }
        return returntime;
     
     }
  • 相关阅读:
    java获取指定月份有几个星期x,获取指定月份跨了多少个星期
    linux下vim编辑器使用
    bash Shell条件测试
    grep与正则表达式
    网络基础--NAT
    网络基础-DHCP
    Python--元组(tuple)
    Python--元组(tuple)
    Linux--用户管理
    Linux--用户管理
  • 原文地址:https://www.cnblogs.com/pastor/p/5156787.html
Copyright © 2011-2022 走看看