I have a number of items with dates specified in MM/dd
format, with no year specified.
However, the year is implied from the context of the data, since the data shows dates no older than 12 months from the "current date" (which is specified in the data)
For example, let's say the current date is January 31, 2013. This means that there will be information from February 1 2012 to January 31, 2013, inclusive.
The problem I'm facing here is, because there are no years specified in the data, I will need to generate the years myself before I load them into my database.
From the context of the data, we know that any dates greater than the current month is from the previous year, while any dates less than or equal to the current month is the current year.
So assuming current date is Jan 2013, we have things like
01/31 - 2013
01/01 - 2013
12/31 - 2012
02/29 - 2012
Now, the problem here is the date on the last line.
2012 was a leap year, so February 29 does exist. However, 2013 is not.
My current approach to date parsing is as follows (using SimpleDateFormat
)
- Grab the date: 01/31
- Append the current year to it: 01/31/2016
- Parse the date using date format
MM/dd/yyyy
Date parsing is performed under strict mode, so something like 02/29 isn't going to be rolled over to 03/01.
However, this algorithm fails on leap years, because assuming the current year is 2013, I'm going to try to parse 02/29/2013
and it will fail.
What is an approach I can use to determine the year of the date?