Fork me on GitHub
#clojure-nl
<
2022-09-22
>
Thierry12:09:44

Hi all, I'm having trouble understanding how to call Java instances methods from within Clojure. Since I have no previous Java experience I'm kind of mind boggled and the explanations I found are puzzling. I found this link (http://xahlee.info/clojure/clojure_calling_java.html) which explains how to use java methods with examples in Java code and Clojure code, e.g.: Java's object_name.method_name(args) would become (. object_name method_name args) or (.method_name object_name args). So far so good and I can get these to work:

(.format
          (java.text.SimpleDateFormat.
           "MMMM"
           (new java.util.Locale "nl", "NL"))
          (.parse
           (java.text.SimpleDateFormat.
            "yyyy-MM-dd")
           "2022-07-01")) ; => "juli"
-
(let [df (java.time.format.DateTimeFormatter/ofPattern "MMMM" (new java.util.Locale "nl", "NL"))
               date (java.time.LocalDate/now)]
           (.format df date)) ; => "september"
But I cannot for the life of me figure out how to call the instance method getLocale from java.time.format/DateTimeFormatter https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#getLocale-- It says here that its public Locale getLocale() where Locale is a link that leads to java.util.Locale but that class doesnt have getLocale... I'm confused

borkdude13:09:42

@thierry572

(let [df (java.time.format.DateTimeFormatter/ofPattern "MMMM" (new java.util.Locale "nl", "NL"))]
  (.getLocale df))

borkdude13:09:16

getLocale is an instance method, so you call it on an instance of DateTimeFormatter

👀 2
✅ 1
Thierry13:09:18

Ahhh thanks! Thanks makes much more sense haha