【问题标题】:Get the amount of days from and to Android获取往返 Android 的天数
【发布时间】:2017-09-01 21:10:31
【问题描述】:

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

我想获取日历上某个日期的绝对天数。 E.G - 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 天

编辑:

我需要另一种方法来做到这一点,因为它给了我负值,我交换了 to 和 from 减号。但是,例如,当我执行第 29 到第 3 时,我遇到了问题。有没有其他方法可以做到这一点?

【问题讨论】:

  • 在“天数之间”的严格定义中,假设您的意思是在一天中的同一时间开始和结束,即中午到中午或 00:00 到 00:00,12 月 1 日到 12 月6 是 5 天。从 12 月 1 日 00:00 开始到 12 月 2 日 00:00 正好是 24 小时或一天。如果您确实希望间隔从第一天的 00:00 开始并在最后一天的 24:00 结束,您需要在“天数”结果中添加 1。另外,代码太多了,正如我在对重复问题的回答中指出的那样,在 Java 中它是微不足道的。
  • 是的,所以如果我们使用严格的定义来计算中午到中午,这将是更多的晚数,例如预订房间,这意味着 12 月 1 日到 12 月 6 日是五。但是,如果我们计算病假天数,那么这将是总共 6 天,对吗?在代码中,我尝试添加 diff += 1 但这不起作用。
  • 您为重复答案提供的链接也是 ChronoUnit,它仅用于更高的 api 级别,我正在尝试导入它,但它无法在 Android Studio 上解决。
  • 如果您使用的是 Java 8,它应该是可用的。你使用的是什么版本的 Java?
  • 日期间隔很棘手,因为有多种解释它们的方法。所有 API 都使用数学定义,因此如果您想要不同的解释,您需要解释在间隔中添加 1 不起作用的地方。您实际得到了什么结果,为什么不可接受?

标签: java android android-layout android-fragments android-datepicker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2020-10-22
    • 2014-03-08
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多