End-date greater than start-date validation androi

2019-04-30 01:24发布

I have two EditText's. One with start date and other with end date. I need to make a validation and check if end date is greater than start-date. I don't know how i can do this.

In my code i make the difference bettween two dates in days, and now i also need to check if end date is greater than start-date

Here is my code:

             //EditText with string of start date
            dataInicio = (EditText)findViewById(R.id.ses_dpDataIni);
             //EditText with string of end date
    dataFim = (EditText)findViewById(R.id.ses_dpDataFim);

             //Convert String to calendar to check the difference between two dates..
    try{
    dateInic = dataInicio.getText().toString();
    dateFim = dataFim.getText().toString();

    calInic=Calendar.getInstance();
    calFim = Calendar.getInstance();
    calInic.setTime(form.parse(dateInic));
    calFim.setTime(form.parse(dateFim));
    }
     catch (ParseException e) { 
         e.printStackTrace(); 
    } 
    Log.w(SessaoQuotaEdit.class.getName(),"DIFERENCA DE DIAS"  +daysBetween(calInic,calFim));
    tvDiasQuotas = (TextView)findViewById(R.id.ses_tvNumDiasQuota);
    tvDiasQuotas.setText("NUMBER OF DAYS: " +daysBetween(calInic,calFim));

            //CHECK IS END-DATE IS GREATER THAN START-DATE
             .............
             .............

Can you help me? Thanks :)

7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-04-30 02:07

Simplified Answer of Ram Kiran, It will check in one statement only.

   public static boolean checkDates(String d1, String d2) {
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        boolean b = false;
        try {
            b = dfDate.parse(d1).before(dfDate.parse(d2)) || dfDate.parse(d1).equals(dfDate.parse(d2));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return b;
    }
查看更多
登录 后发表回答