//计算 2016-08-20 到2016-08-26之间隔了多少天?1833天

1.思路获取两个日期之间间隔多少毫秒! 一天 = 1*24*60*60*1000=86400000毫秒

  public static void main(String[] args) throws Exception {
    //计算 2016-08-20  到2016-08-26之间隔了多少天?
        String from = "2011-08-20";
        String to = "2016-08-26";
        String patternDate = "yyyy-MM-dd";
        System.out.println(getDiffOfTheDate(from,to ,patternDate ));

    }

    public static String getDiffOfTheDate(String from, String to, String patternDate) throws Exception{
        DateFormat format = new SimpleDateFormat(patternDate);
        long dateFrom = format.parse(from).getTime();//1471622400000
        long dateTo = format.parse(to).getTime(); //1472140800000
        long diff = Math.abs(dateTo-dateFrom); //158371200000
        long dayDiff = diff/86400000;
        return dayDiff+"";
    }
}

 

posted @ 2016-08-26 15:48  黑土白云  阅读(256)  评论(0编辑  收藏  举报