Search code examples
clojurejodatimeclj-time

parse doesn't accept all month names abbreviation


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"]                                                                                                                                                                                                                                           
                 ]                                            

Solution

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