Reputation: 31390
I want to have objects that define partial dates / times similar to the entries in Unix crontabs, i. e. "every first day in any month" etc.
These objects are going to be compared to Calendar objects to see if they match.
I was about to write my own "TimePattern" class but it would have to contain a lot of logic like checking if the field values were inside legal bounds (hour between 0 and 23 etc) that I felt would be inside classes like Calendar anyway.
So, now I'm just using a (nonlenient) Calendar object with some fields unset and I want to compare them with fully set Calendar objects that represent a fixed point in time. Should I
Upvotes: 2
Views: 2339
Reputation: 9941
One solution that I've used in the past is to write a DateFormat that will format dates with only the fields that you want, then compare the resulting Strings. For example, it's quite common not to care about the time component, only dates, so you might do something like this:
public boolean sameDate(Calendar c1, Calendar c2)
{
DateFormat datesOnly = new SimpleDateFormat("yyyy-MM-dd");
return datesOnly.format(c1.getTime()).equals(datesOnly.format(c2.getTime()));
}
It's probably not the best implementation if you're going to create a new library but it works pretty well for other cases.
Upvotes: 1
Reputation: 31390
This is what I did eventually:
public class CalendarMatch<T extends Calendar> {
private T _dt;
public CalendarMatch(T dt) {
this._dt = dt;
}
public boolean fMatches(T dtCmp)
{
int[] rgChecks = {
Calendar.HOUR_OF_DAY,
Calendar.MINUTE,
Calendar.SECOND,
Calendar.DAY_OF_MONTH,
Calendar.MONTH,
Calendar.YEAR,
Calendar.DAY_OF_WEEK
};
for (int ixField : rgChecks) {
// if any field in our partial date is set
// but does not equal its counterpart, it's not a match
if (this._dt.isSet(ixField) && dtCmp.get(ixField) != this._dt.get(ixField)) {
return false;
}
}
// otherwise, it is
return true;
}
}
Upvotes: 0
Reputation: 4540
Would Google's RFC-2445 be of any use? It's a date recurrence calculator that lets you easily do the "first monday of every third month" semantics.
Upvotes: 0
Reputation: 17546
You could create a set of Comparators - MinuteMatch, HourMatch, etc, and use them to compare the two dates?
Upvotes: 0