结束日期大于开始日期验证android

时间:2012-09-06 11:00:20

标签: android validation date calendar

我有两个EditText。一个有开始日期,另一个有结束日期。 我需要进行验证并检查结束日期是否大于开始日期。我不知道怎么做到这一点。

在我的代码中,我在两个日期之间做出差异,现在我还需要检查结束日期是否大于开始日期

这是我的代码:

             //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
             .............
             .............
你能帮帮我吗? 谢谢:))

10 个答案:

答案 0 :(得分:19)

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;
}

答案 1 :(得分:3)

尝试此功能。

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;
        }
    }

如果enddate在开始日期之后,则返回true。

答案 2 :(得分:0)

答案 3 :(得分:0)

感谢大家......

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");

答案 4 :(得分:0)

***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..**

答案 5 :(得分:0)

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;
    }
}

答案 6 :(得分:0)

Ram Kiran的简化答案,它只会在一个声明中检查。

   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;
    }

答案 7 :(得分:0)

public static boolean CheckDates(String d1, String d2){
    SimpleDateFormat dfDate  = new SimpleDateFormat("yyyy-MM-dd");
    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;
}

答案 8 :(得分:0)

Kotlin扩展名:


#include<iostream>
#include<string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int *test1();

int *test2();

char *test3();

char *test4();

const char *test5();

const char *test6();

const char *test7();


int main() {

//uncomment only one test at a time and run it; ouput shown in comment

//   cout<<*test1()<<endl;//SIGSEGV

//    cout << *test2() << endl;//output


//    char *ptr = test3();
//    while (*ptr)//SIGSEGV
//        cout << *ptr++;
//    cout << endl;

//    char *ptr = test4();
//    while (*ptr)//OUTPUT!!!
//        cout << *ptr++;
//    cout << endl;

//    const char *ptr = test5();
//    while (*ptr)//SIGSEGV
//        cout << *ptr++;
//    cout << endl;


//    const char *ptr = test6();
//    while (*ptr)//OUTPUT!!!
//        cout << *ptr++;
//    cout << endl;

//    const char *ptr = test7();
//    while (*ptr)//OUTPUT!!!
//        cout << *ptr++;
//    cout << endl;


}


int *test1() {


   int a = 123456;
   return &a;
}





int *test2() {


   int a = 112233;
   int *b = &a;  
   return b;
}

char *test3() {

   char ptr[] = "ab";

   return ptr;
}

char *test4() {

   char ptr[] = "ab";
   char *ptr2 = ptr;

   return ptr2;
}

const char *test5() {
   const char ptr[] = "ab";

   return ptr;
}

const char *test6() {
   const char ptr[] = "ab";
   const char *ptr2 = ptr;
   return ptr2;
}

const char *test7() {
   const char *ptr = "ab";
   return ptr;
}



答案 9 :(得分:0)

          Calendar startdate = Calendar.getInstance();
            Calendar enddate = Calendar.getInstance();
                  imgstartcal1.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                fromDatePickerDialog = new DatePickerDialog(mainActivity, new DatePickerDialog.OnDateSetListener() {
                                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


                                        startdate.set(year, monthOfYear, dayOfMonth);
                                        edtstartdate.setText(dateFormatter.format(startdate.getTime()));
                                       edtenddate.setText(null);

                                    }
                                }, startdate.get(Calendar.YEAR), startdate.get(Calendar.MONTH), startdate.get(Calendar.DAY_OF_MONTH));
                                fromDatePickerDialog.show();
                            }
                        });

          imgendcal1.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
          fromDatePickerDialog = new DatePickerDialog(mainActivity, new DatePickerDialog.OnDateSetListener() {
          public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

          enddate.set(year, monthOfYear, dayOfMonth);
          if (startdate.after(enddate) || enddate.equals(startdate)){                                
          edtenddate.getText().toString().equals(null);
          }else{                                                  
    fromDatePickerDialog.getDatePicker().setMinDate(startdate.getInstance().getTimeInMillis());                                              
          edtenddate.setText(dateFormatter.format(enddate.getTime()));
            }
            }
  }, enddate.get(Calendar.YEAR), startdate.get(Calendar.MONTH),startdate.get(Calendar.DAY_OF_MONTH));
    fromDatePickerDialog.show();
    }
   });