Compare Two String Date and Time in android?

user2688507 picture user2688507 · Sep 10, 2013 · Viewed 55.4k times · Source

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 these datetimes and i need result.

Answer

user1831682 picture user1831682 · Sep 10, 2013
try{

     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");


     String str1 = "12/10/2013";
     Date date1 = formatter.parse(str1);

     String str2 = "13/10/2013";
     Date date2 = formatter.parse(str2);

     if (date1.compareTo(date2)<0)
      {
         System.out.println("date2 is Greater than my date1");                         
      }

    }catch (ParseException e1){
        e1.printStackTrace();
    }