获取从 Android 到 Android 的天数

Get the amount of days from and to Android

我在计算总天数时总是得到错误的数额。

我想获取日历上某个日期从和到此的绝对天数。例如 - 12 月 1 日至 12 月 6 日应为 6 天,9 月 15 日至 12 月 17 日应为 3 天。但这并没有给我想要的结果,我也不认为我这样做是最好的方式:(

代码:

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");
    }

}

我正在尝试在 onDateSet 方法中执行此操作。

谢谢

编辑:

我试过这个:

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

我得到的结果很奇怪:

第 1 到第 4 - 给我 2 天 - 应该给我 4

13 日到 20 日给我 6 天 - 应该给我 8 天

编辑:

我需要另一种方法来执行此操作,因为它给了我负值我交换了减号和减号。但是,例如,当我执行第 29 到第 3 次时,我遇到了问题。还有其他方法吗?

使用 Joda 时间解决了这个问题:

添加了对 build.gradle 的依赖:

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

增加下面的天数以提供准确的天数:

            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++;