0

I have two functions wherein it lets the user set a particular date. It's Start Date and End Date. Now I need to compare these two variables because the Start Date should not be greater than the End Date. Here's my code in android. Any help will do. Thanks.

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {
        yr = year;
        month = monthOfYear;
        day = dayOfMonth;
        Date.setText((month + 1) + "/" + day + "/" + yr);
    }
};

private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view1, int year1, int monthOfYear1, int dayOfMonth1)
    {
        yr = year1;
        month = monthOfYear1;
        day = dayOfMonth1;

        DateEnd.setText((month + 1) + "/" + day + "/" + yr);
    }
};
user3319349
  • 15
  • 1
  • 4

4 Answers4

0

You can check this by following code.

if (mDateSetListener.before(mDateSetListener1) || Code.DueDate.equals(Code.AssignDate)) {
    AlertDialog.Builder alertDialogBuilderDate = new AlertDialog.Builder(Assignment_Create_Ext_DB.this);
    alertDialogBuilderDate.setTitle("Date assigning issue");
    alertDialogBuilderDate
    .setMessage("Due date can not be equal or less then the Assign date")
    .setCancelable(false)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
    alertDialogBuilderDate.show();
}
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • as stated `mDateSetListener.before(mDateSetListener1)` you can also use `mDateSetListener.after(mDateSetListener1)` if you want to set date after. – InnocentKiller Feb 19 '14 at 12:41
0

Following is my code to compare two dates,

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

String startDate = tvStartDate.getText().toString().trim(); 
String endDate = tvEndDate.getText().toString().trim();

startDate = startDate.substring( startDate.indexOf( ":" ) + 1 ); 
endDate = endDate.substring( endDate.indexOf( ":" ) + 1 );

Date startDat = sdf.parse(startDate); 
Date endDat = sdf.parse(endDate);

if ( startDat.compareTo(endDat) <= 0 )
{
     // ok
}
else
{
    Toast.makeText(getApplicationContext(), "Start date must be lesser than or equal to End Date", Toast.LENGTH_SHORT ).show();
}
user3322955
  • 348
  • 1
  • 15
0

Just convert your date string to date object as:

 try{       
                SimpleDateFormat curFormater = new SimpleDateFormat(dateFormat.getValue(),locale); 
                Date d1 = curFormater.parse(dateStr); 
Date d2 = curFormater.parse(dateStr2); 

//Now just compare the two dates as

if(d1.before(d2)){
}
else{
}
            }catch (Exception e) {
                LogHandler.error(tag, "getDataFormate", e);
                return null;
            }

Let me know if it's helpful.

jitain sharma
  • 584
  • 5
  • 19
0
    boolean compareTime(int startDay, int startMonth, int startYear,
        int monthDay, int month, int year) {
    Time timeNow = new Time();
    Time timeSelected = new Time();

    timeNow.set(startDay, startMonth, startYear);

    timeSelected.set(monthDay, month, year);

    long millisNow = timeNow.toMillis(true);
    long millisSelected = timeSelected.toMillis(true);

    long diff = millisSelected - millisNow;
    long diffHours = diff / (60 * 60 * 1000);

    Log.i(TAG, "diffHours = " + diffHours);

    if (diffHours <= 0) {
        return true;
    } else {
        return false;
    }
}
naidu
  • 350
  • 2
  • 8