Does anyone know of a DateFormatter in Java that isn’t so picky? By that I mean I could provide it multiple formats the date could be written in and if I provide it a format such as:
yyyy-MM-dd HH:mm:ss Z
A user could enter:
2010-11-02 10:46:05 -0600
or
2010-11-02 10:46:05
or
2010-11-02 10:46
or
2010-11-02
or
2010-11-02 -0600
I could create an implementation of DataFormat that is configured with a List of DateFormat objects and make my implementation run through each in the List until one is able to parse the date. So, I really am just curios if someone is aware of an existing date formatting library that is less picky/more flexible then what Java provides.
It looks like you have a pretty regular format for each part, it’s just that the parts are optional. I would use a regex that has each part (some being optional). Match on that regex and get the groups for each part. Then put them together in the most complete form (“2010-11-02 10:46:05 -0600”) and have the DateFormatter parse that. This way you also get to control the defaults for the parts if they are missing.
Here is some code:
Output: