Maxime Tant Maxime Tant - 1 month ago 16
Android Question

Android : Substract 2 dates

Using a picker time I get a date:

DatePicker dp =(DatePicker)findViewById(;
int v1 = dp.getMonth();
int v2 = dp.getDayOfMonth();
int v3 = dp.getYear();

After I get the date of today in a string:

String datejour = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

How can I substract date from the datepicker and date of dotay?

Considering that the date picker is higher than the other.
Thanks for your help :)


How to subtract two dates and calculate difference date.

Suppose you have two dates

        String str_date1=  "27/10/2016"; //todays date
        String str_date2=  "10/11/2016"; 

This solution for calculate days difference:

        Date date1;
        Date date2;

        SimpleDateFormat dates = new SimpleDateFormat("MM/dd/yyyy");

        //Setting two dates
        date1 = dates.parse(str_date1);
        date2 = dates.parse(str_date2);

        //Comparing two dates
        long difference = Math.abs(date1.getTime() - date2.getTime());
        long differenceDates = difference / (24 * 60 * 60 * 1000);

        //Convert long to String
        String dayDiff = Long.toString(differenceDates);

        Log.e("Days","Number: " + dayDiff);

This solution for compare two dates :

        if (date1.after(date2)) {
     // your operation

I recommended to use this solution. May be it will help.