Compare Two String Date and Time in android?

前端 未结 9 638
北海茫月
北海茫月 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:25

    Try this,

    String pattern = "<yourPattern>";
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    try {
     Date one = dateFormat.parse(<yourDate>);
     Date two = dateFormat.parse(<yourDate>);
    
    }
    catch (ParseException e) {}
    
    0 讨论(0)
  • 2020-12-15 05:28
    public static String CompareDates(String date1, String date2) {
    try{
    String pattern = "dd-MMM-yyyy HH:mm:ss";
    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    Date Date1 = formatter.parse(date1);
    Date Date2 = formatter.parse(date2);
    if (Date1 != null && Date2 != null) {
    if (Date1 .equals(Date2 ))
    { 
    //Both dates are equal.
    } 
    else{
    //both date are not equal("use after and before to check occurrence of dates")
    }
    }catch (Exception e1){
    e1.printStackTrace();
    }
    }
    
    0 讨论(0)
  • 2020-12-15 05:30
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date1 = sdf.parse("2010-02-22");
                Date date2 = sdf.parse("2010-03-23");
                int i = date1.compareTo(date2);
                switch (i){
                    case -1: //date1<date2 = -1
                        return sDate2;
                    case 1: //date1>date2 = 1
                        return sDate1;
                    case 0: //date1==date2= 0
                    default:
                        return sDate2;
                }
            } catch (ParseException e) {
                return sDate1;
            }
    
    0 讨论(0)
提交回复
热议问题