Y2K Dates Out Of Control!

Wow check this out I was digging in a java code base and ran into this date conversion code (added the dateString initialization to illustrate):

String dateString = "04/29/05" ;   // two digit year
 String newDateString = dateString.substring(0, length - 2);
 if (Integer.parseInt(year.substring(2)) > 25) {
      newDateString += "19";
 } else {
     newDateString += "20";
 }
newDateString += dateString.substring(length - 2);
 dateString = newDateString;

LOL.  So if the year is greater than 25, it’s the 1900’s, else its the 2000’s.  :-/

I know that dates suck big time in Java, but this code was result of just bad design.  Across it is all types of date input formats, and no centralized date code and this garbage all over.  Death by 1000 cuts.

There are tons of utilities like Joda Time all over to handle this stuff.  But if they knew the six digit date, they were safe just doing the conversion and formatting it with SimpleDateFormat into a date and then extracting the string!  Even this would have been easier

    DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
    Date date = (Date)formatter.parse(dateString);

Or something to that effect.  I’m pretty sure there are no 1924 records in the database.

Comments are closed.