Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End-date greater than start-date validation android

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 :)

like image 572
Ricardo Filipe Avatar asked Sep 06 '12 11:09

Ricardo Filipe


People also ask

How to check end date is greater than start date in android?

setText("NUMBER OF DAYS: " +daysBetween(calInic,calFim)); //CHECK IS END-DATE IS GREATER THAN START-DATE ............. .............

How do you validate the end date greater than the start date?

Go to Solution. function dateCheck(){ let startDate = new Date($("#hero_startdate"). val()); let endDate = new Date($("#hero_enddate"). val()); if(startDate > endDate) { alert("End date need to be bigger then start date"); } } $( document ).

How do I validate a date?

The date validator requires day, month and year. If you are looking for hour and time validator, HH:mm , for example, you should use the regexp validator. Below are some example of possible formats: YYYY/DD/MM.

How do you validate start date and end date in react JS?

You can pass min and max in the normal HTML input tag with format YYYY-MM-DD . This serves as min and max for date. To format the date you can use date-fns library. Check the sandbox and see once you select the start date all the previous dates are disabled.


2 Answers

Try this function.

public static boolean isDateAfter(String startDate,String endDate)
    {
        try
        {
            String myFormatString = "yyyy-M-dd"; // for example
            SimpleDateFormat df = new SimpleDateFormat(myFormatString);
            Date date1 = df.parse(endDate));
            Date startingDate = df.parse(startDate);

            if (date1.after(startingDate))
                return true;
            else
                return false;
        }
        catch (Exception e) 
        {

            return false;
        }
    }

It return true if enddate is after start date.

like image 38
Chirag Avatar answered Oct 19 '22 06:10

Chirag


SimpleDateFormat dfDate  = new SimpleDateFormat("yyyy-MM-dd");
public static boolean checkDates("2012-07-12", "2012-06-12)"    {
    boolean b = false;
    try {
        if(dfDate.parse(d1).before(dfDate.parse(d2)))
        {
            b = true;//If start date is before end date
        }
        else if(dfDate.parse(d1).equals(dfDate.parse(d2)))
        {
            b = true;//If two dates are equal
        }
        else
        {
            b = false; //If start date is after the end date
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return b;
}
like image 91
Ram kiran Pachigolla Avatar answered Oct 19 '22 06:10

Ram kiran Pachigolla