Java Date Difference getDateDiff(String startDate, String endDate)

Here you can find the source of getDateDiff(String startDate, String endDate)

Description

get Date Diff

License

Apache License

Declaration

public static long getDateDiff(String startDate, String endDate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {

    public static long getDateDiff(String startDate, String endDate) {
        long diff = 0;
        try {/*from  www.  j av a 2 s  .c  om*/
            Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
            Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);

            diff = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0
                    ? (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000)
                    : (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
        }
        return diff;
    }

    public static long getDateDiff(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return 0L;
        }
        long diff = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0
                ? (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000)
                : (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
        return diff;
    }
}

Related

  1. diffMillis(Date d1, Date d2)
  2. diffMonth(Date before, Date after)
  3. diffMonth(Date start, Date end)
  4. diffOfDate(String begin, String end)
  5. diffY(Date endDate, Date startDate)
  6. getDateDiff(String startDt, String endDt)
  7. getDateDiff(String startTime, String endTime)
  8. getDateDiffDay(String begindate, String enddate)
  9. getDateDifference(String start, String end)