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).