* allow comparison with String representation of SimpleDate

This commit is contained in:
Reinhard Pointner 2014-06-01 05:03:31 +00:00
parent 478123552f
commit ddee292af5
1 changed files with 23 additions and 1 deletions

View File

@ -10,7 +10,7 @@ import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
public class SimpleDate implements Serializable {
public class SimpleDate implements Serializable, Comparable<Object> {
private int year;
private int month;
@ -54,6 +54,24 @@ public class SimpleDate implements Serializable {
return super.equals(obj);
}
@Override
public int compareTo(Object other) {
if (other instanceof SimpleDate) {
return compareTo((SimpleDate) other);
} else if (other instanceof CharSequence) {
SimpleDate otherDate = parse(other.toString());
if (otherDate != null) {
return compareTo(otherDate);
}
}
throw new IllegalArgumentException(String.valueOf(other));
}
public int compareTo(SimpleDate other) {
return Long.compare(this.getTimeStamp(), other.getTimeStamp());
}
@Override
public int hashCode() {
return Arrays.hashCode(new Object[] { year, month, day });
@ -77,6 +95,10 @@ public class SimpleDate implements Serializable {
return new SimpleDateFormat(pattern, locale).format(new GregorianCalendar(year, month - 1, day).getTime()); // Calendar months start at 0
}
public static SimpleDate parse(String string) {
return parse(string, "yyyy-MM-dd");
}
public static SimpleDate parse(String string, String pattern) {
if (string == null || string.isEmpty())
return null;