/** * Get a diff between two dates * * @param oldDate * the old date * @param newDate * the new date * @return the diff value, in the days */ publicstaticlonggetDateDiff(SimpleDateFormat format, String oldDate, String newDate){ try { return TimeUnit.DAYS.convert( format.parse(newDate).getTime() - format.parse(oldDate).getTime(), TimeUnit.MILLISECONDS); } catch (Exception e) { e.printStackTrace(); return0; } }
使用方式:
1 2 3
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); long dateDiff = getDateDiff(sdf, "4/12/2017", "6/12/2017"); System.out.println(" days diff is : " + dateDiff);