Compare Two String Date and Time in android?

前端 未结 9 712
北海茫月
北海茫月 2020-12-15 04:54

I have Two String Datetime as follows:

String Date1 = \"05-09-2013 10:46:10\"
String Date2 = \"06-09-2013 10:46:10\"

I Need to compare thes

9条回答
  •  再見小時候
    2020-12-15 05:19

    this snippet explain when we compare two dates in string formats, in other words to date is not allowed when greater than from date.

     private void setTodate() {
                // TODO Auto-generated method stub
                // Process to get Current Date
                int fYear, fMonth, fDay;
                final Calendar c = Calendar.getInstance();
                fYear = c.get(Calendar.YEAR);
                fMonth = c.get(Calendar.MONTH);
                fDay = c.get(Calendar.DAY_OF_MONTH);
                // Launch Date Picker Dialog
                DatePickerDialog dpd = new DatePickerDialog(RemindMeDetails.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @SuppressLint("SimpleDateFormat")
                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {                  
                            try{                        
                                SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");        
                                String fdate=recr_edit_Fromdate.getText().toString();                       
                                Date dateFD = formatter.parse(fdate);                       
                                String tdate=(dayOfMonth + "-"
                                        + (monthOfYear + 1) + "-" + year).toString();
                                Date dateTD = formatter.parse(tdate);                       
                                if (dateFD.compareTo(dateTD)<=0)
                                  {
                                    /* System.out.println("tdate is Greater than my fdate");  
                                     recr_edit_Todate.setText(dayOfMonth + "-"
                                                + (monthOfYear + 1) + "-" + year);*/
                                    recr_edit_Todate.setText(tdate);
                                  }
                                else{
                                    showmessage("Alert", "Start date is not greater than End date");                            
                            }catch (ParseException e1){
                                e1.printStackTrace();
                            }   
                            }
                        }, fYear, fMonth, fDay);
                dpd.show();
            }// method closed
    

提交回复
热议问题