3

I'm using Java 7 and Clojure 1.4 for this.

I'm writing up some database tests in Clojure for a table that contains Date objects, using OracleDB over JDBC.

I need to compare the date I receive (a Date object) with a String - so presumably I need to convert that string to a Date object. After some googling, I found Java's SimpleDateFormat.

This is what I use (with extra stuff for debugging)

(defn parseDate [date]
  (do (debug (str "Parsing date: " date ))
    (let [ dateobj (java.text.SimpleDateFormat. "dd-MMM-YY")
           parsed (do (. dateobj setLenient false) (. dateobj parse date))]
      (debug (str "Result: " parsed)) parsed)))

I throw in some dates, and I get the following output..

Parsing date: 01-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 01-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012
Parsing date: 00-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 02-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012

That doesn't seem right, at all.

The Date object returned is something like this: #<Date Mon Jan 02 00:00:00 GMT 2012>, which is clearly not equal to what I get back from the database, for example, #<Date 2012-01-01>.

Does anyone have any ideas about this?

NOTE: I get the same result whether I use setLenient or not (and with either true or false).

Answer (Courtesy of Jon Skeet Link to answer)

I was using YY in my format string, where I should have actually used yy (Since Y is the week-year and y is the simple year).

Community
  • 1
  • 1
gdude2002
  • 197
  • 9

1 Answers1

6

I believe the problem is that you're using YY instead of yy. From the docs of SimpleDateFormat, YY refers to the week-year rather than the year.

Week-years are somewhat odd, and unless you know you want to use it, you probably don't. In particular, mixing week-year with "day of month" and "month" is almost never appropriate. You'd normally use "week-year, week-of-year, day-of-week" together.

To put it another way: the two systems are a bit like RGB and HSV for colours; it's as if you've defined a format of "red, green, hue" which doesn't make a lot of sense :)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Yep, now my Date objects have the correct date, you are indeed correct. They still don't equal the ones in the database, but I presume I should be able to modify them to match. I'll accept this one when it lets me. – gdude2002 Jul 18 '12 at 09:30