0

I have a date picker through which I am setting up a date in string i.e. mDate. Now to set up time I want to check if current date and the mDate is same if they are same I want to set minTime to TimePicker dialog and if they are not same min time should not be set to time.

If mDate is empty still time should show all time.

   mEditTxt_Time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date date = null;
                try {

                    if(!mDate.equals("")) {
                        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
                        date = format.parse(mDate);
                    }

                    Calendar compareDate = Calendar.getInstance();
                    compareDate.getTime();

                Calendar now = Calendar.getInstance();

                TimePickerDialog tpd = TimePickerDialog.newInstance(
                        PostShippingFragment.this,
                        now.get(Calendar.HOUR_OF_DAY),
                        now.get(Calendar.MINUTE),
                        true
                );

                    if(date!=null) {
                        if (date.compareTo(compareDate.getTime())) {
                            tpd.setMinTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), now.get(Calendar.SECOND));
                        }
                    }
                    else {

                        tpd.setVersion(TimePickerDialog.Version.VERSION_2);

                        tpd.setAccentColor(ContextCompat.getColor(getActivity(), R.color.colorAccent));

                        tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface dialogInterface) {
                                Log.d("TimePicker", "Dialog was cancelled");
                            }
                        });


                        tpd.show(getFragmentManager(), "Timepickerdialog");
                    }
            }
                catch (ParseException e)
                {
                    Log.e("exception",e.toString());

                }
            }
        });

I tried this but I am not able to do the comparison.

Please help. Thank you.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Sid
  • 2,792
  • 9
  • 55
  • 111

2 Answers2

4

You can use compareTo() method of Date.

It will return an integer value,

  • return a value 0 if the argument Date (compareDate) is equal to this Date;
  • return a value less than 0 if this Date is before the Date argument (compareDate);
  • return a value greater than 0 if this Date is after the Date argument (compareDate).

So, you need to modify your if condition as below, if you want to check both date are same or not.

if (date.compareTo(compareDate.getTime()) == 0) {
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
0

Use any of the following method to compare the dates

Method 1

  • date1.after(date2)
  • date1.before(date2)
  • date1.equals(date2)

Method 2

if(date1.compareTo(date2)>0){
  Log.d("Date", "Date1 is after Date2");
}else if(date1.compareTo(date2)<0){
    Log.d("Date", "Date1 is before Date2");
}else{
      Log.d("Date", "Date1 is equal to Date2");
}

Method 3

Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
  • cal1.after(cal2)
  • cal1.before(cal2)
  • cal1.equals(cal2)
Jeffrey Rajan
  • 4,391
  • 4
  • 27
  • 37