Subtle But Deadly Java Date Bug

By | May 28, 2011

All of the college professors who have never worked in the real world oooh and aaahhh over Java. I’ve never been a big fan of the language, in part because it is one of the most inefficient languages on the planet, but mostly, because any tool in the marketplace with that much hype surrounding it generally turns out to be a shiny new turd. (How many of you are old enough to remember PowerBuilder? How about all of those different “Case Tool” products which were going to completely eliminate programmers?)

I was forced to use Java on a recent project and came face to face with a known bug which has been allowed to exist for a very long time. Please examine the following:


  1. import*;
  2. import java.util.Date;
  3. import java.util.Calendar;
  4. import java.text.*;
  5. public class badDateBugs {
  6. public static void main(String args[]){
  7. DateFormat screenFmt = new SimpleDateFormat( “yyyy/MM/dd”);
  8. DateFormat badScreenFmt = new SimpleDateFormat( “yyyy/mm/dd”);
  9. String original_date_str = “2008/09/01”;
  10. System.out.println( “Original date ” + original_date_str);
  11. System.out.println( “Result when good date format string used”);
  12. Calendar d1 = Calendar.getInstance();
  13. try {
  14. d1.setTime( screenFmt.parse( original_date_str));
  15. d1.add( Calendar.DATE, -7); // last week
  16. System.out.println( “One week prior was ” + screenFmt.format( d1.getTime()));
  17. } catch( ParseException p) {
  18. System.out.println( “Error parsing date1”);
  19. }
  20. Calendar d2 = Calendar.getInstance();
  21. System.out.println( “\n\nResult when bad date format string used”);
  22. try {
  23. d1.setTime( badScreenFmt.parse( original_date_str));
  24. d1.add( Calendar.DATE, -7); // last week
  25. System.out.println( “One week prior was ” + screenFmt.format( d1.getTime()));
  26. } catch( ParseException p) {
  27. System.out.println( “Error parsing date2”);
  28. }
  29. } // end main method
  30. } // end class badDateBugs


You probably don’t even notice the subtle typo which causes this problem. Listing lines 11 and 12 contain it. All you have to do is enter “mm” instead of “MM” and this bug rears its ugly head.

roland@logikaldesktop:~/java_date_example$ java badDateBugs
Original date 2008/09/01
Result when good date format string used
One week prior was 2008/08/25

Result when bad date format string used
One week prior was 2007/12/25


You will notice that none of the much vaulted “ exceptions” were thrown. The parse engine simply went on its way, and Java was happy to give you the wrong answer without any indication of a problem. Nice, very nice.


Category: Information Technology Tags: , ,

About seasoned_geek

Roland Hughes started his IT career in the early 1980s. He quickly became a consultant and president of Logikal Solutions, a software consulting firm specializing in OpenVMS application and C++/Qt touchscreen/embedded Linux development. Early in his career he became involved in what is now called cross platform development. Given the dearth of useful books on the subject he ventured into the world of professional author in 1995 writing the first of the "Zinc It!" book series for John Gordon Burke Publisher, Inc. A decade later he released a massive (nearly 800 pages) tome "The Minimum You Need to Know to Be an OpenVMS Application Developer" which tried to encapsulate the essential skills gained over what was nearly a 20 year career at that point. From there "The Minimum You Need to Know" book series was born. Three years later he wrote his first novel "Infinite Exposure" which got much notice from people involved in the banking and financial security worlds. Some of the attacks predicted in that book have since come to pass. While it was not originally intended to be a trilogy, it became the first book of "The Earth That Was" trilogy: Infinite Exposure Lesedi - The Greatest Lie Ever Told John Smith - Last Known Survivor of the Microsoft Wars When he is not consulting Roland Hughes posts about technology and sometimes politics on his blog. He also has regularly scheduled Sunday posts appearing on the Interesting Authors blog.