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?
-
check this out please, mkyong.com/java/how-to-compare-dates-in-javaWilliam Kinaan– William Kinaan2013-03-15 10:14:29 +00:00Commented Mar 15, 2013 at 10:14
Add a comment
|
5 Answers
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) { }
}
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
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
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
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