Problems with DatePikerDialog when comparing dates

1

I am using the DatePikerDialog to publish news from my android app, this dialog is launched by a button in which the publication date is chosen, which has to be greater than or equal to the current date , if this is smaller, skip a snackbar of information.

The fact is that when the date, that is, the day coincides with the current day, sometimes the snackbar jumps (something that did not have to happen) and sometimes not, and I do not know what this may be due to. In the same code I make a TimePickerDialog jump but that in principle does not give problem. I attach the code in question:

final Calendar fEle = Calendar.getInstance();
        final Calendar cal = Calendar.getInstance();

        final ArrayList total = new ArrayList();


        DatePickerDialog.OnDateSetListener odsl = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                final Calendar hEle = Calendar.getInstance();
                hEle.set(Calendar.YEAR, i);
                hEle.set(Calendar.MONTH, i1);
                hEle.set(Calendar.DAY_OF_MONTH, i2);

                DateFormat formato=DateFormat.getDateInstance();
                total.add(formato.format(new Date(hEle.getTimeInMillis()))+" ");

                if (cal.before(fEle) || cal.equals(fEle)) {


                    TimePickerDialog.OnTimeSetListener otsl = new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker timePicker, int i, int i1) {

                            hEle.set(fEle.get(Calendar.YEAR), fEle.get(Calendar.MONTH), fEle.get(Calendar.DATE), i, i1);

                            if (cal.equals(fEle)) {

                                if (cal.getTime().after(hEle.getTime())) {
                                    Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.errFechaEmpiece, Snackbar.LENGTH_LONG)
                                            .show();
                                } else {
                                    total.add(i + ":" + i1);
                                }

                            } else {
                                total.add(i + ":" + i1);
                            }

                            if(total.size()>=2) {
                                cambiarLaFechaDePublicacion((String) total.get(0), (String) total.get(1));
                            }

                        }
                    };

                    TimePickerDialog dialog = new TimePickerDialog(getContext(), otsl,
                            java.util.Calendar.getInstance().get(java.util.Calendar.getInstance().HOUR),
                            java.util.Calendar.getInstance().get(java.util.Calendar.getInstance().MINUTE),
                            true);
                    dialog.show();


                } else {
                    Snackbar.make(getActivity().findViewById(android.R.id.content), R.string.errFechaEmpiece, Snackbar.LENGTH_LONG)
                            .show();
                }


            }


        };

        DatePickerDialog dpd = new DatePickerDialog(getActivity(), odsl,
                java.util.Calendar.getInstance().get(java.util.Calendar.getInstance().YEAR),
                java.util.Calendar.getInstance().get(java.util.Calendar.MONTH),
                java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH));
        dpd.show();

Thanks and best regards.

    
asked by Adrián Garrido Blázquez 19.09.2017 в 17:32
source

2 answers

0

I leave the log with the different passes.

  Log.v("Data cal ", cal.getTime().toString());
  Log.v("Data fEle ", fEle.getTime().toString());

        //Condicion original
        if (cal.before(fEle) || cal.equals(fEle)) {

Priemra call I select date 9/23/17, current time.

V / Data cal: Sat Sep 23 09:34:33 GMT-03: 00 2017
V / Data fEle: Sat Sep 23 09:34:33 GMT-03: 00 2017

Exit:
V / Data: changePublication Date

Second call I selected 9/21/17, two days before. Observe the values of cal and fEle before going through the if

V / Data cal: Sat Sep 23 09:37:40 GMT-03: 00 2017
V / Data fEle: Sat Sep 23 09:37:40 GMT-03: 00 2017

Exit:
V / Data: changePublication Date

Third call selected 25/9/17, two days after today's date.

V / Data cal: Sat Sep 23 09:40:06 GMT-03: 00 2017
V / Data fEle: Sat Sep 23 09:40:06 GMT-03: 00 2017

Exit:
V / Data: changePublication Date

That is, the condition always compares two equal values.

Now the outputs with the modified code as marked before ..

 Log.v("Data cal ", cal.getTime().toString());
 Log.v("Data hEle ", hEle.getTime().toString());

       //Condición modificada
        if (cal.before(hEle) || cal.equals(hEle)) {

First call I select date 9/23/17, current time. Note the difference of seconds.

V / Data cal: Sat Sep 23 09:44:13 GMT-03: 00 2017
V / Data hEle: Sat Sep 23 09:44:18 GMT-03: 00 2017

Exit:
V / Data: changePublication Date

Second call I selected 9/21/17, two days before. It leaves by the Snapbar of the DatePickerDialog pq the date is previous.

V / Data cal: Sat Sep 23 09:45:55 GMT-03: 00 2017
V / Data hEle: Thu Sep 21 09:45:58 GMT-03: 00 2017

Exit:
V / Data: SnackBar2

Third call selected 25/9/17, two days after today's date.
V / Data cal: Sat Sep 23 09:47:35 GMT-03: 00 2017
V / Data hEle: Mon Sep 25 09:47:39 GMT-03: 00 2017

Exit:
V / Data: changePublication Date

The condition within the TimePickerDialog

    if (cal.equals(fEle)) {

It is also always true because of what is seen in the first part. Greetings.

    
answered by 23.09.2017 / 15:17
source
0

I observe this in your code.

final Calendar fEle = Calendar.getInstance();
final Calendar cal = Calendar.getInstance();

The first condition is always true.

if (cal.before(fEle) || cal.equals(fEle)) {

The two calendar objects were created at the same time, that is, you should always enter the TimePickerDialog.

The error is in not comparing cal with hEle which would be the new date.

if (cal.before(hEle) || cal.equals(hEle)) {
   //Si la fecha seleccionada es igual o posterior
}else{
   //Si la fecha seleccionada es anterior
}

Another option you have to avoid this error is to deactivate the previous dates. Setting the setMinDate property of the DatePickerDialog object, as shown in the example.

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    Date dd = null;
    try {
        dd = sdf.parse("21/9/2017");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    dpd = new DatePickerDialog(this,odsl,a,m,d);
    dpd.getDatePicker().setMinDate(dd.getTime());

Greetings.

    
answered by 20.09.2017 в 22:48