日期选择器中的Android拆分错误

时间:2018-01-18 20:36:49

标签: java android datepicker

大家好!我正在创建一个酒店预订移动应用程序,我需要的大多数模块是设置签入和签出。我想设置从登记日期选择器中选择结帐的注意事项。我在检查中获取所选日期的值时发现错误。有人可以帮助我吗?

   etin = findViewById(R.id.etcheckin);
    etin.setInputType(InputType.TYPE_NULL);
    etout = findViewById(R.id.etcheckout);
    etout.setInputType(InputType.TYPE_NULL);

    etin.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            showTruitonDatePickerDialog(view);

        }
    });

    etout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            showToDatePickerDialog(view);

        }
    });
}

public void showTruitonDatePickerDialog(View v){
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public void  showToDatePickerDialog(View v){
    DialogFragment newFragment = new ToDatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public static class  DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar c = Calendar.getInstance();

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);

        datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
        c.set(2018,11 ,31);
        datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
        return datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month , int day) {
        // Do something with the date chosen by the user
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(year, month - 0 +1 , day);

        etin.setText(month+1 + "/" + day + "/" + year);
        etout.setText(month+ 1 + "/" + day + "/" + year);

        String monthString = null;
        String [] monthsTemp = {"Jan", "Feb","March","April","May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};

        if (month == 0) {
            monthString = monthsTemp[0];
        }
        else if (month == 1){
            monthString = monthsTemp[1];
        }
        else if (month == 2){
            monthString = monthsTemp[2];
        }

        else if (month == 3){
            monthString = monthsTemp[3];
        }

        else if (month == 4){
            monthString = monthsTemp[4];
        }

        else if (month == 5){
            monthString = monthsTemp[5];
        }

        else if (month == 6){
            monthString = monthsTemp[6];
        }

        else if (month == 7){
            monthString = monthsTemp[7];
        }

        else if (month == 8){
            monthString = monthsTemp[8];
        }

        else if (month == 9){
            monthString = monthsTemp[9];
        }

        else if (month == 10){
            monthString = monthsTemp[10];
        }

        else{
            monthString = monthsTemp[11];
        }

        etin.setText(monthString + "/" + day + "/" + year);

    }
}

public static class ToDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

       String getfromdate = etin.getText().toString();
      String getfrom[] = getfromdate.split("/");

        Calendar c = Calendar.getInstance();
        int year,month,day;
        year= Integer.parseInt(getfrom[2]);
        month = Integer.parseInt(getfrom[0]);
       day = Integer.parseInt(getfrom[1]);

        c.set(year, month, day + 1);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.Theme_AppCompat_DayNight_Dialog_Alert, this, year, month, day);

        datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());

        c.set(2018,11, 31);
        datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
        return datePickerDialog;

    }
    public void onDateSet(DatePicker view, int year, int month, int day) {

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(0);
        cal.set(year, month , day);

        etout.setText(new StringBuilder().append(month).append("/").append(day).append("/").append(year));


    }
}

0 个答案:

没有答案