Reputation: 39859
I'm having an hard time trying to parse date in the future, and I would apreciate some help!
Here's the thing, I'd like to add a parsed date to the current date (to have it in the future). The problem is that I can have many kind of date format, like :
So if the user set something like 5
, the returned date will be (in our timelapse) 2011-11-05
.
If he set 02-14
, it will be 2012-02-14
.
But suppose we are the 4th of november
, and the user set 11-03
, it will be 2012-11-03
and not 2011-11-03
since it's past.
I tried to play with Calendar, Date, SimpleFormat, but I cannot make it work.
My parsers (using SimpleDateFormat) are working though.
Could you help me archieve this? I'm not asking for a complete code, just something that would set me on the right track!
thanks! :)
Upvotes: 0
Views: 93
Reputation: 240900
Since you have fixed list of acceptable input date format, set the lenient field of dateFormat
to false
and check to see if one of them satisfy your work done or if exception is raised go for next pattern
dateFormat = new SimpleDateFormat(PATTERN_ONE);
dateFOrmat.setLenient(false);
dateFormat.parse(INPUT_STRING);
// if an exception is caughtm try with next pattern
Upvotes: 2
Reputation: 1247
I have had very good experience with jodatime - http://joda-time.sourceforge.net/. Checkout the Dateformatters in that.
It has a very extensive API and lets you do things like add and subtract dates - taking into account timezones and daylight saving etc.
Upvotes: 1