Friday, January 20, 2006

Bit by SimpleDateFormat

Maybe I'm just lame, but mainly I think I just assumed I knew more than I really did.

Beware of using the following...
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

You would think that you are doing yourself a favor and that the above would require a user to enter four digits. That is not true. What happens is that if there is four y's entered the year is assumed to be literal. Meaning that if a user enters 1/1/05, the Date will end up being Jan 01, 0005. Not cool.

So, according to the javadoc...

SimpleDateFormat Javadoc

You are better off using a MM/dd/yy pattern. Why?
Well, when the year pattern only specifies only 2 characters, the 80/20 rule is applied to the year. The 2 digits are considered against 80 years prior and 20 years after the date the SimpleDateFormat was created on. Thus 1/1/05 becomes Jan 01, 2005.

Bass Ackwards I say, but oh well...