如何计算android中两个datepicker之间的天数

时间:2016-06-14 10:29:44

标签: android datepicker calendar android-datepicker

当用户在date之间选择date时。如何calculate在Android中的第3 Edittext个晚上? 按照我的datepicker

代码
3 edittext box

1起日期 2迄今为止 3夜数=?

 CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate);

        CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate);





        CheckInDate.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {


                if (v == CheckInDate) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    c.set(Calendar.DAY_OF_MONTH, mDay);


                    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    Calendar cal = Calendar.getInstance();
                                    cal.set(Calendar.MONTH, monthOfYear);
                                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                    cal.set(Calendar.YEAR, year);
                                    if(cal.before(c)) {
                                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    StringBuilder date = new StringBuilder();
                                    date.delete(0, date.length());
                                    date.append((dayOfMonth<10?"0":"")).append(dayOfMonth)
                                            .append("/").append((monthOfYear + 1) < 10 ? "0" : "")
                                            .append((monthOfYear+1)).append("/").append(year);
                                    CheckInDate.setText(date.toString());
                                }
                            }, mYear, mMonth, mDay);
                    dpd.getDatePicker().setMinDate(c.getTimeInMillis());
                    dpd.show();
                }

            }

        });

        CheckOutDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (v == CheckOutDate) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    c.set(Calendar.DAY_OF_MONTH, mDay);


                    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    Calendar cal = Calendar.getInstance();
                                    cal.set(Calendar.MONTH, monthOfYear);
                                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                    cal.set(Calendar.YEAR, year);
                                    if(cal.before(c)) {
                                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    StringBuilder date = new StringBuilder();
                                    date.delete(0, date.length());
                                    date.append((dayOfMonth<10?"0":"")).append(dayOfMonth)
                                            .append("/").append((monthOfYear + 1) < 10 ? "0" : "")
                                            .append((monthOfYear+1)).append("/").append(year);
                                    CheckOutDate.setText(date.toString());
                                }
                            }, mYear, mMonth, mDay);
                    dpd.getDatePicker().setMinDate(c.getTimeInMillis());
                    dpd.show();                }

            }

        });

2 个答案:

答案 0 :(得分:0)

试试这个 -

Date checkInTime, checkOutTime;
 private void checkDiff() {
    long duration = checkOutTime.getTime() - checkInTime.getTime();

    long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
    long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
    long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
    //Now Code as per requirement
}


        CheckInDate = (EditText) rootView.findViewById(R.id.CheckInDate);
        CheckOutDate = (EditText) rootView.findViewById(R.id.CheckOutDate);

        CheckInDate.setOnClickListener(new View.OnClickListener()

        {
            @Override
            public void onClick(View v) {


                if (v == CheckInDate) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    c.set(Calendar.DAY_OF_MONTH, mDay);

                    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    Calendar cal = Calendar.getInstance();
                                    cal.set(Calendar.MONTH, monthOfYear);
                                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                    cal.set(Calendar.YEAR, year);
                                    if (cal.before(c)) {
                                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    checkInTime = cal.getTime();
                                    StringBuilder date = new StringBuilder();
                                    date.delete(0, date.length());
                                    date.append((dayOfMonth < 10 ? "0" : "")).append(dayOfMonth)
                                            .append("/").append((monthOfYear + 1) < 10 ? "0" : "")
                                            .append((monthOfYear + 1)).append("/").append(year);
                                    CheckInDate.setText(date.toString());
                                }
                            }, mYear, mMonth, mDay);
                    dpd.getDatePicker().setMinDate(c.getTimeInMillis());
                    dpd.show();
                }

            }

        });

        CheckOutDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (v == CheckOutDate) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    c.set(Calendar.DAY_OF_MONTH, mDay);
                    checkOutTime = c.getTime();

                    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    Calendar cal = Calendar.getInstance();
                                    cal.set(Calendar.MONTH, monthOfYear);
                                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                    cal.set(Calendar.YEAR, year);
                                    if (cal.before(c)) {
                                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    StringBuilder date = new StringBuilder();
                                    date.delete(0, date.length());
                                    date.append((dayOfMonth < 10 ? "0" : "")).append(dayOfMonth)
                                            .append("/").append((monthOfYear + 1) < 10 ? "0" : "")
                                            .append((monthOfYear + 1)).append("/").append(year);
                                    CheckOutDate.setText(date.toString());
                                    checkOutTime = cal.getTime();
                                    checkDiff();
                                }
                            }, mYear, mMonth, mDay);
                    dpd.getDatePicker().setMinDate(c.getTimeInMillis());
                    dpd.show();
                }

            }

        });
    }

答案 1 :(得分:0)

尝试使用此代码获取总天数:

  Calendar cal1 = new GregorianCalendar();
     Calendar cal2 = new GregorianCalendar();

     SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");

     Date date = sdf.parse("your first date");
     cal1.setTime(date)
     date = sdf.parse("your second date");
     cal2.setTime(date);

 System.out.println("Days= "+daysBetween(cal1.getTime(),cal2.getTime()));

在此处检查您的天数代码:

        CheckOutDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (v == CheckOutDate) {

                    final Calendar c = Calendar.getInstance();
                    int mYear = c.get(Calendar.YEAR);
                    int mMonth = c.get(Calendar.MONTH);
                    int mDay = c.get(Calendar.DAY_OF_MONTH);
                    c.set(Calendar.DAY_OF_MONTH, mDay);


                    DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {
                                    Calendar cal = Calendar.getInstance();
                                    cal.set(Calendar.MONTH, monthOfYear);
                                    cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                                    cal.set(Calendar.YEAR, year);
                                    if(cal.before(c)) {
                                        Toast.makeText(getActivity(), "Select valid date please.", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                    StringBuilder date = new StringBuilder();
                                    date.delete(0, date.length());
                                    date.append((dayOfMonth<10?"0":"")).append(dayOfMonth)
                                            .append("/").append((monthOfYear + 1) < 10 ? "0" : "")
                                            .append((monthOfYear+1)).append("/").append(year);
                                    CheckOutDate.setText(date.toString());




                                    /************* Check you days HERE ***************/
                                 try {

                                    Calendar calCheckIn =  new GregorianCalendar();
\                                    Calendar calCheckOut = new GregorianCalendar();

                                     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");


                                    Date dateCheckIn = sdf.parse(CheckInDate.getText().toString());
                                    calCheckIn.setTime(dateCheckIn);
                                    Date dateCheckOut = sdf.parse(CheckOutDate.getText().toString());
                                    calCheckOut.setTime(dateCheckOut);

                                    System.out.println("Days= "+daysBetween(dateCheckIn.getTime(),dateCheckOut.getTime()));

                                     EditText Night = (EditText) rootView.findViewById(R.id.Noofnights); 
                                       Night.setText(daysBetween(dateCheckIn.getTime(),dateCheckOut.getTime()) + "");
                                    } catch (Exception e) {
                                    e.printStackTrace();
                                  }
/***************************************/


                                }
                            }, mYear, mMonth, mDay);
                    dpd.getDatePicker().setMinDate(c.getTimeInMillis());
                    dpd.show();                }

            }

        });

将此方法放在课堂上的任何位置:

 public int daysBetween(Date d1, Date d2){
             return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
     }