The Java-based application that Dan M. supports does something that is frequently accomplished by applications the world over - based on the value of a passed string containing a valid date, convert it to datetime. Simple stuff. Java even has built-ins to make this task even easier.

Well, the developer behind the below code decided to take the idea of date conversion using Java's built-ins and run with it ...way off of the reservation.

How? First, and this is probably just a 'nice to have' as far as enterprise code goes, but you can't specify which date format to use with the passed value. Instead, hope that you correctly chose one of the possible combinations that are caught in the nested try/catch "design pattern"...but even when you do, other awesomeness appears.

Say you want to convert "04/09/14" to Wed Apr 09 00:00:00 GMT 2014 stored in a Date variable. No problem, that works fine. But what about "04-09-14" (MM-DD-YY) or "04/09" (MM/DD) - I mean, they're called out in the code so they should be handled, right? You bet they are...just add the current year at the end!

04-09-14 becomes 04-09-14 2014 (which Java parses correctly somehow), but MM/DD dates like 04/09 unsurprisingly fail to convert after being changed to 04/09 2014-2014/2014. Now, that's some fancy date mangling right there.

Well, at least the function is well-documented.


/**
 * Converts a String to a Date using Java's DateFormat class.
 * @param dateString
 * @return
 */
public static java.util.Date convertToDateUsingFormat(String dateString) {
  if(dateString!=null){
    DateFormat dateFormat = new SimpleDateFormat("M/d/y");
    try {
      return dateFormat.parse(dateString);
    } catch (ParseException e) {
      // July 10, 2009
      dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
      try {
        return dateFormat.parse(dateString);
      } catch (ParseException e2) {
        // Jul 10, 2009
        dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
        try {
          return dateFormat.parse(dateString);
        } catch (ParseException e3) {
          // Jul 09
          dateFormat = new SimpleDateFormat("M y");
          try {
            return dateFormat.parse(dateString);
          } catch (ParseException e4) {
            // short
            dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
            try {
              return dateFormat.parse(dateString);
            } catch (ParseException e5) {
              // M d
              dateFormat = new SimpleDateFormat("M d");
              try {
                return dateFormat.parse(dateString);
              } catch (ParseException e6) {
                // M d (add current year)
                dateFormat = new SimpleDateFormat("M d y");
                dateString += " " + Calendar.getInstance().get(Calendar.YEAR);
                try {
                  return dateFormat.parse(dateString);
                } catch (ParseException e7) {
                  // M-d (add current year)
                  dateFormat = new SimpleDateFormat("M-d-y");
                  try {
                    return dateFormat.parse(dateString);
                  } catch (ParseException e8) {
                    // M-d (add current year)
                    dateFormat = new SimpleDateFormat("M-d-y");
                    dateString += "-" + Calendar.getInstance().get(Calendar.YEAR);
                    try {
                      return dateFormat.parse(dateString);
                    } catch (ParseException e9) {
                      // M/d (add current year)
                      dateFormat = new SimpleDateFormat("M/d/y");
                      dateString += "/" + Calendar.getInstance().get(Calendar.YEAR);
                      try {
                        return dateFormat.parse(dateString);
                      } catch (ParseException e10) {
                        e10.printStackTrace();
                        return null;
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  } else return null;
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!