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条回答
Ridiculous、
2楼-- · 2019-04-30 01:46
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;
}
查看更多
叛逆
3楼-- · 2019-04-30 01:48
***Used Ram kiran Answer***     

public boolean checkDatesBefore(String startDate, String endDate) {
            boolean b = false;
            SimpleDateFormat dfDate = new SimpleDateFormat("MM/dd/yyyy");
            try {
                if (dfDate.parse(startDate).before(dfDate.parse(endDate))) {
                    b = true;// If start date is before end date
                } else if (dfDate.parse(startDate).equals(dfDate.parse(endDate))) {
                    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;
        }

    checkDatesBefore(startDate,endDate);


    **and check another time for not less than or equals today**

    public boolean checkDatesAfter(String selectedDate, String today) {
            boolean b = false;
            SimpleDateFormat dfDate = new SimpleDateFormat("MM/dd/yyyy");
            try {
                if (dfDate.parse(selectedDate).compareTo((dfDate.parse(today))) < 0)

           {
                    b = true;// If start date is before end date
                } else if (dfDate.parse(selectedDate).equals(dfDate.parse(today))) {
                    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;
        }
        enter code here

    call checkDatesAfter(selectedDate,today)

    **Best of luck..**
查看更多
Juvenile、少年°
4楼-- · 2019-04-30 01:57

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.

查看更多
疯言疯语
5楼-- · 2019-04-30 01:57
你好瞎i
6楼-- · 2019-04-30 02:05

Thanks to everyone ....

Here is my solution

        if(calInic.after(calFim)==true)
        Log.w(SessaoQuotaEdit.class.getName(),"ERROR: DATA FINAL É ANTES DA DATA INICIAL");
    else
        if(calInic.before(calFim)==true)
            Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS CORRECTAS");
        else
            if(calInic.equals(calFim)==true)
                Log.w(SessaoQuotaEdit.class.getName(),"CORRECTO: DATAS IGUAIS");
查看更多
The star\"
7楼-- · 2019-04-30 02:05
if (!dateValidation("2016-11-23", "2016-11-24", "yyyy-MM-dd")) {
    // ToastHelper.show("Please select date properly");

    // or do something
}


public static boolean dateValidation(String startDate,String endDate,String dateFormat )
{
    try
    {

        SimpleDateFormat df = new SimpleDateFormat(dateFormat);
        Date date1 = df.parse(endDate);
        Date startingDate = df.parse(startDate);

        if (date1.after(startingDate))
            return true;
        else
            return false;
    }
    catch (Exception e)
    {
        return false;
    }
}
查看更多
登录 后发表回答