Search code examples
javaandroidandroid-layoutandroid-fragmentsandroid-datepicker

Get the amount of days from and to Android


I keep getting the wrong amount on my calculation of getting the amount of total days.

I want to get the absolute number of days from and to a certain date on my calendar. E.G - 1st Dec to 6th Dec should be 6 days, 15th September to 17th December should be 3 days. But it's not giving me the results I want nor do I think I am doing this the best way :(

Code:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.borax12.materialdaterangepicker.date.DatePickerDialog;

import java.util.Calendar;

/**
 * Created by hp1 on 21-01-2015.
 */
public class Tab2 extends Fragment implements DatePickerDialog.OnDateSetListener{

    DatePicker pickerDate;
    TextView dateTextView;
    private boolean mAutoHighlight;
    Calendar to;
    Calendar from;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.tab2,container,false);

        Button dateButton = (Button)v.findViewById(R.id.button2);
        dateTextView = (TextView)v.findViewById(R.id.textView5);

        CheckBox ahl = (CheckBox) v.findViewById(R.id.autohighlight_checkbox);
        ahl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                mAutoHighlight = b;
            }
        });

        // Show a datepicker when the dateButton is clicked
        dateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                to = Calendar.getInstance();
                DatePickerDialog dpd = com.borax12.materialdaterangepicker.date.DatePickerDialog.newInstance(
                        Tab2.this,
                        to.get(Calendar.YEAR),
                        to.get(Calendar.MONTH),
                        to.get(Calendar.DAY_OF_MONTH)
                );
                dpd.setAutoHighlight(mAutoHighlight);
                dpd.show(getActivity().getFragmentManager(), "Datepickerdialog");

                dpd.setYearRange(2016, 2017);
            }

        });

        int year,monthofyear,dayofmonth;
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        DatePickerDialog dpd = (DatePickerDialog) getActivity().getFragmentManager().findFragmentByTag("Datepickerdialog");
        if(dpd != null) dpd.setOnDateSetListener(this);
    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth,int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {

        to.set(Calendar.YEAR, year);
        to.set(Calendar.MONTH, monthOfYear);
        to.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        from = Calendar.getInstance();

        from.set(Calendar.YEAR, yearEnd);
        from.set(Calendar.MONTH, monthOfYearEnd);
        from.set(Calendar.DAY_OF_MONTH, dayOfMonthEnd);

        long diff = to.get(Calendar.DAY_OF_MONTH) - from.get(Calendar.DAY_OF_MONTH);
        Log.e("Amount of Days is ", "" +   diff);

        Log.e(dayOfMonth + "TOOOOOOOOO" + dayOfMonthEnd, "DDDDDDDDDDDDDDDDDDDDDDDDDDDDD" + Math.abs(diff));

        String date = "You picked the following date: From- "+dayOfMonth+"/"+(++monthOfYear)+"/"+year+" To "+dayOfMonthEnd+"/"+(++monthOfYearEnd)+"/"+yearEnd;
        dateTextView.append(new StringBuilder().append(date) + "\n");
    }

}

I'm trying to do this within the onDateSet method.

Thanks

Edit:

I've tried this:

long diff = to.get(Calendar.DAY_OF_MONTH) - from.get(Calendar.DAY_OF_MONTH);
diff = diff + 1;

The results I am getting are strange as I get:

1st to 4th - Gives me 2 days - should give me 4

13th to 20th Gives me 6 days - should give me 8 days

Edit:

I need another way of doing this as it was giving me minus values I swapped the to and the from minus. But I have an issue when I do 29th to 3rd for instance. Is there another way I can do this?


Solution

  • Solved this by using the Joda time:

    Added the dependency to build.gradle:

    compile 'net.danlew:android.joda:2.9.9'
    

    Increment the days below to give me the exact days:

                to.set(Calendar.YEAR, year);
                to.set(Calendar.MONTH, monthOfYear);
                to.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    
                from = Calendar.getInstance();
                from.set(Calendar.YEAR, yearEnd);
                from.set(Calendar.MONTH, monthOfYearEnd);
                from.set(Calendar.DAY_OF_MONTH, dayOfMonthEnd);
    
                Date toTime = to.getTime();
                Date fromTime = from.getTime();
    
                int days2 = Days.daysBetween(new DateTime(toTime), new DateTime(fromTime)).getDays();
                days2++;