【问题标题】:End-date greater than start-date validation android结束日期大于开始日期验证android
【发布时间】:2012-08-31 04:28:49
【问题描述】:

我有两个 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
             .............
             .............

你能帮帮我吗? 谢谢:)

【问题讨论】:

    标签: android validation date calendar


    【解决方案1】:
    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;
    }
    

    【讨论】:

    • 谢谢,你拯救了我的一天。
    • 我正在使用 kotlin,而我的一直给我例外。你能帮帮我吗?
    【解决方案2】:

    试试这个功能。

    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。

    【讨论】:

      【解决方案3】:
      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;
      }
      

      【讨论】:

      • 一些解释或代码 cmets 会改进答案。
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        谢谢大家....

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

        【讨论】:

          【解决方案6】:
          ***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..**
          

          【讨论】:

            【解决方案7】:
            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;
                }
            }
            

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:

                Kotlin 扩展:

                fun String.isLessThanEndDate(toDate:String):Boolean {
                        val dfDate = SimpleDateFormat("dd MMM, yyyy", Locale.ENGLISH)
                        var result = false
                        try
                        {
                            result = dfDate.parse(this).before(dfDate.parse(toDate)) || dfDate.parse(this) == dfDate.parse(toDate)
                        }
                        catch (e:ParseException) {
                            e.printStackTrace()
                        }
                        return result
                    }
                

                【讨论】:

                  【解决方案10】:
                            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();
                      }
                     });
                  

                  【讨论】:

                  • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-12-05
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-06-30
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-10-24
                  相关资源
                  最近更新 更多