Skip to content Skip to sidebar Skip to footer

Compare Date And Time In String Format

I have a string with date and time in this format: 9 Oct 2014 07:09:17 GMT Is there an easy way to convert this string into a comparable object, to compare with the current date,

Solution 1:

 SimpleDateFormat formatter = new SimpleDateFormat("d MMM yyyy kk:mm:ss zzz");  
 String str1 = "9 Oct 2014 07:09:17 GMT";
 Date date1 = formatter.parse(str1);

Solution 2:

Try using this:

try{
     SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
     String str1 = "your first date";
     Date date1 = formatter.parse(str1);

     String str2 = "Your second date";
     Date date2 = formatter.parse(str2);

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

Post a Comment for "Compare Date And Time In String Format"