Question: I'd like to implement a filter/search feature in my application using Lucene. 

Querying Lucene index gives me a `Hits` instance, which is nothing more than a list of `Document`s matching my criteria. 

Since I generate the indexed `Document`s from my objects, which is the best way to find the original object related to a specific Lucene `Document`?

---

A better description of my situation: 

* Three model classes for now: `Folder` (can have other `Folder`s or
`List`s as children), `List` (can have `Task`s as children) and
`Task` (can have other `Task`s as children). They are all
`DefaultMutableTreeNode` subclasses. I'll add the `Tag` entity in the
future.
* Each `Task` has a text, a start date, a due date, some boolean flags.
* They are displayed in a `JTree`.
* The hole tree is saved in an XML file.
* I'd like to do things like these:
* search `Task`s with Google-like queries.
* Find all `Task`s that start today.
* Filter `Task`s by `Tag`.

Answer: 
One way to avoid incorrect dates in User input, is not to let them happen in the first instance, i.e. use a date picker control.

What about using [`DateTime.TryParse`](http://msdn.microsoft.com/en-us/library/system.datetime.tryparse.aspx) ?

> 
> Converts the specified string
>  representation of a date and time to
>  its DateTime equivalent using the
>  specified culture-specific format
>  information and formatting style, and
>  returns a value that indicates whether
>  the conversion succeeded.
> 
> 
> 

This [MSDN page](http://msdn.microsoft.com/en-us/library/9h21f14e.aspx) shows examples of usage.
You *should* (morally) not try to handle inputs like this. Is February 29, 1900 to be interpreted as March 1, 1900 (because February 29, 1900 could be interpreted as the day after February 28, 1900 but since it doesn't exist move the actual day after February 28, 1900) or as February 28, 1900 (because February 29, 1900 could be interpreted as the last day of February, 1900)? Another situation is what if the user means to type "2009-3-3" but because of fast and sticky fingers accidentally types "2009-3-33". Then rather than their error being caught, a custom parser will swallow this into, say, 4/2/2009. Because of situations like this, you *should* just `DateTime.TryParse` the input and inform the user if invalid input occurs. That's what you *should* do.

Now, if it's a requirement that you handle such input. I would use something along the lines of the following:

```
static DateTime Parse(int year, int month, int day) {
        DateTime date;
        if (month < 1 || month > 12) {
            int direction = month < 1 ? 1 : -1;
            do {
                month += direction * 12;
                year -= direction;
            } while (month < 1 || month > 12);
        }
        int daysInMonth = DateTime.DaysInMonth(year, month);
        if (day < 1 || day > daysInMonth) {
            date = new DateTime(year, month, daysInMonth);
            int difference = day - daysInMonth;
            date = date.AddDays(difference);
        }
        else {
            date = new DateTime(year, month, day);
        }
        return date;
}

```