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;
     
     }
  • 相关阅读:
    ZendFramwork配置
    JS控制页面前进、后退
    PHP乱码
    php 文件和表单内容一起上传
    mysqli常用命令
    图解SQL多表关联查询
    mysql默认字符集修改
    mysql控制台命令
    Nanami's Digital Board

  • 原文地址:https://www.cnblogs.com/pastor/p/5156787.html
Copyright © 2011-2022 走看看