1

For a given custom-formatter, parse refuses certain month values.

(require '[clj-time.core :as t]
'[clj-time.format :as f]) 


(let [custom-formatter  (f/formatter  "dd MMM yyyy")]
(f/parse custom-formatter  "27 mar 2010")
(f/parse custom-formatter  "27 dec 2010"))

The first parsereturns the expected result, #object[org.joda.time.DateTime 0x62a9d176 "2010-03-27T00:00:00.000Z"], while the second returns Actual: java.lang.IllegalArgumentException: Invalid format: "27 dec 2010" is malformed at "dec 2010".

I can't make sense of this behavior. What can possibly causes this issue?

Thanks for help.

update

Here's my project.cljfile

:dependencies [[org.clojure/clojure "1.7.0"]                                                                                                                                                                                                                                  
                 [twitter-api  "0.7.8"]                                                                                                                                                                                                                                         
                 [cheshire  "5.6.1"]                                                                                                                                                                                                                                            
                 [clojure-csv/clojure-csv "2.0.1"]                                                                                                                                                                                                                              
                 [org.postgresql/postgresql "9.4-1206-jdbc42"]                                                                                                                                                                                                                  
                 [com.jolbox/bonecp  "0.8.0.RELEASE"]                                                                                                                                                                                                                           
                 [org.clojure/java.jdbc  "0.6.1"]                                                                                                                                                                                                                               
                 [java-jdbc/dsl  "0.1.0"]                                                                                                                                                                                                                                       
                 [org.slf4j/slf4j-nop "1.7.21"]                                                                                                                                                                                                                                 
                 [clj-time  "0.11.0"]                                                                                                                                                                                                                                           
                 ]                                            
user3639782
  • 487
  • 3
  • 10
  • The error that is produced does not seem to have to do with the example you posted. It looks like your are trying to parse `"27 oct 2010"` which has an extra space. – kongeor May 30 '16 at 13:36
  • @kongeor you are right. I messed up with my copy/paste. Now, I have updated my post. And the error I describe is indeed experienced with correct formatting. – user3639782 May 30 '16 at 13:43
  • Strange. Your code works for me. Note that you have an extra closing parenthesis. If you throw this code example into a `REPL` it breaks? Which versions are you using? – kongeor May 30 '16 at 14:06
  • Yes, it breaks in the REPL as well. I have provided the dependencies I use. – user3639782 May 30 '16 at 14:24
  • Can you run the tests in this [repo](https://github.com/kongeor/datefmt)? These are the two examples you listed. – kongeor May 30 '16 at 14:47
  • I go the same error message i posted for _dec_ while _mar_ works. – user3639782 May 30 '16 at 15:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113341/discussion-between-kongeor-and-user3639782). – kongeor May 30 '16 at 15:14

1 Answers1

4

You likely need to specify a Locale for your formatter. You can check to see your default Locale with:

user=> (java.util.Locale/getDefault)
#object[java.util.Locale 0x4713d9a5 "en_US"]

See the results for different locales (including the error you are seeing):

user=> (require '[clj-time.core :as t]
  #_=> '[clj-time.format :as f]) 
nil

user=> (let [custom-formatter  (f/with-locale 
                                 (f/formatter  "dd MMM yyyy")
                                 java.util.Locale/ENGLISH)] 
         (f/parse custom-formatter  "27 mar 2010"))
#object[org.joda.time.DateTime 0x3cfceae6 "2010-03-27T00:00:00.000Z"]

user=> (let [custom-formatter  (f/with-locale 
                                 (f/formatter  "dd MMM yyyy")
                                 java.util.Locale/ITALY)] 
         (f/parse custom-formatter  "27 mar 2010"))
#object[org.joda.time.DateTime 0x4f5e9955 "2010-03-27T00:00:00.000Z"]

user=> (let [custom-formatter  (f/with-locale 
                                 (f/formatter  "dd MMM yyyy")
                                 java.util.Locale/CHINA)] 
         (f/parse custom-formatter  "27 mar 2010"))

IllegalArgumentException Invalid format: "27 mar 2010" is malformed at "mar 2010"  org.joda.time.format.DateTimeFormatter.parseDateTime (DateTimeFormatter.java:899)
Curtis Summers
  • 586
  • 5
  • 7