0

I have a problem. I have two strings. Every element has date format. I can use Date.parse(string) to parse string to date. Now I have a problem how can I compare this two strings, because I want to check which date is higher. If I have one string 6-05-2012 and second 12-05-2012 I want to check which date is higher(in this case will be 12-05-2012). How I can do that? Maybe is other way, without parsing to date?

1

5 Answers 5

2

Do it like this.

You can use the after and before method also.

Check here.

public void compareDate() {
    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31");
        Date date2 = sdf.parse("2010-01-31");

        if(date1.compareTo(date2)>0){
            Log.v("app","Date1 is after Date2");
        }else if(date1.compareTo(date2)<0){
            Log.v("app","Date1 is before Date2");
        }else if(date1.compareTo(date2)==0){
            Log.v("app","Date1 is equal to Date2");
        }
    } catch(Exception e) { }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Downvoter why you downvote it. You should not do this thing without letting to mistake..
I agree with @TGMCians you should give the explanation why you down voted. If it is for a valid reason I second otherwise not.
1

Try this --

String dtStart = "2010-10-15T09:27:37Z";  
SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");  
try {  
    Date date = format.parse(dtStart);  
    System.out.println(date);  
} catch (ParseException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
}

OR

public static long compareTo( java.util.Date date1, java.util.Date date2 )  
{  
//returns negative value if date1 is before date2  
//returns 0 if dates are even  
//returns positive value if date1 is after date2  
  return date1.getTime() - date2.getTime();  
} 

String strDate1 = "10-JAN-1920";  
String strDate2 = "10-APR-1950";  

SimpleDateFormat sdf = new SimpleDateFormat( "dd-MMM-yyyy" );  
java.util.Date d1 = sdf.parse( strDate1 );  
java.util.Date d2 = sdf.parse( strDate2 );  

System.out.println( "1. " + sdf.format( d1 ).toUpperCase() );  
System.out.println( "2. " + sdf.format( d2 ).toUpperCase() );  

if ( compareTo( d1, d2 ) < 0 )  
{  
   System.out.println( "d1 is before d2" );  
}  
else if ( compareTo( d1, d2 ) > 0 )   
{  
   System.out.println( "d1 is after d2" );  
}  
else  
{  
   System.out.println( "d1 is equal to d2" );  
}  

Comments

1

I this another best way to compare dates it to use date class itself

 void dateComparator() throws ParseException {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Date date1 = sdf.parse("2009-12-31");
      Date date2 = sdf.parse("2010-01-31");

      if (date1.before(date2)) {
         System.out.println("Date1 is before Date2");
      } else if (date1.after(date2)) {
         System.out.println("Date1 is after Date2");
      } else if (date1.equals(date2)) {
         System.out.println("Date1 is equal to Date2");
      } else {
         System.out.println("How to get here?");
      }
   }

Comments

1
private Date compareDates(final String dateString)
{

    SimpleDateFormat  format = new SimpleDateFormat("dd-MM-yyyy");  
    Date date = null;
    try {  
        format.parse(dateString);  
    } catch (Exception e) {  
        Log.d(getClass().getName(), e.getMessage());           
    }
    return date;
}

Now you can compare between two dates. Hope this will help for more reference to SimpleDateFormat please check here. Noe you can use date.getTime() to compare your dates.

Comments

0

You better off using Joda time. See http://joda-time.sourceforge.net/index.html for more information. The standard API is somehow limited and complicated and forces you to reinvent already made methods made in Joda time

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.