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.
Leave a Comment » |
Software | Tagged: Java, programming, Software, Utilities |
Permalink
Posted by rolfje
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 »
Leave a Comment » |
Apple, Fun, Hardware | Tagged: Apple, design, Fun, iphone, opinion |
Permalink
Posted by rolfje
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 »
8 Comments |
Hardware, Workshop | Tagged: Garmin, Hardware, Speed Triple, triumph, Zumo 660 |
Permalink
Posted by rolfje