Simple Strict Date Parsing

Saturday 2010-03-06

In Java, the DateFormat.parse() method is a funny little critter. It helps you by trying to figure out what date you actually meant when you typed in “35/12/2O10″ (note the letter “O” in 2O10). In this case, it will parse the date without errors or warnings, and returns the date “11/12/04″ (November 12th, 0004). That’s because it thinks “35″ is a month, and “2″ is the year, ignoring everything after the letter “O”.

DateFormat tries to convert the “35th month” into 2 years and 11 months, and correct the date accordingly. df.setLenient(false) prevents this, but that still leaves the problem of the parsing stopping at the first wrong character without warning.

I needed a much stricter way of parsing dates, and yesterday I found an elegant solution to this problem. It’s so small I was able to tweet it in less than 140 characters, but I thought it deserved a decent blogpost so here it goes:

public Date parseDateString(String inputDateString)
         throws ParseException {
  DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
  Date parsedDate = df.parse(inputDateString);

  if (!inputDateString.equals(df.format(parsedDate))) {
    throw new ParseException("Invalid Date", 0);
  }
  return parsedDate;
}

The brilliance here is in the comparing of the formatted date with the original input. The method returns a normal ParseException so you can perfectly replace your original df.parse() calls with it, making them more strict.

Thanks to Bas for this elegant and simple solution.


Ibatis Inline Parameter Maps

Tuesday 2010-02-16

Today I (re)discovered a feature in the Ibatis data mapper framework which was clearly documented, but for some reason was not being used in our project. The feature is called “inline parameter maps” and combined with a wrapper bean it can clean up a lot of clutter in the code and in the SqlMaps. Please feel free to share this example with your fellow Ibatis Data Mapper 2 framework users.

Read the rest of this entry »


The rumors can stop now. iPad is here.

Wednesday 2010-01-27

So the word is finally out, it’s going to be the iPad. I was kind of worried about this, and my worries are not completely taken away by the news currently oozing out of the Apple keynote. I see some problems with this device, although they are well hidden under Steve’s reality distortion field, the Apple secret sauce and the “gee why didn’t we think of that” features.

Read the rest of this entry »


The Car Buying Story

Sunday 2010-01-10

Stephen needs a new car. Being dilligent and a car lover, he takes the time to write down all criteria for a great car. When he finishes the list, he notices that the only car matching all his criteria is a Ferrari. Well, he always loved a Ferrari so that makes sense. To make things more realistic he adds another criteria, called “price”, and starts looking for alternatives.

Read the rest of this entry »


Zūmo Mount for Triumph Speed Triple

Wednesday 2009-12-30

The RAM mount coming with the Garmin zūmo’s are fine, but I wanted something more elegant to go with my Triumph Speed Triple. This article contains a detailed description of building your own mount for the Garmin zūmo 660. I mounted the zūmo on my Speed Triple, but the mount is universal enough to be center-mounted on any bike which has riser-mounted handlebars.

No guarantees ofcourse, you’re on your own from here. Bring your tools and read on.

WARNING: When I tried to cut one of these rubbers in half, the bottom disk came loose. It was not as securely glued to the rubber as the top disk. When pulling the zūmo on the mount it does not come off, but if you want to be sure order the Garmin 550 mount rubbers (described in article below).

Read the rest of this entry »