datetimeclojurecalendarbabashka

Clojure: what's the way to have current time string with babashka with least dependency?


With the following expression,

bb '(new java.util.Date)'
#inst "2020-07-18T14:35:16.663-00:00"

I want to get a string, and slightly formatted as:

"2020-07-18 UTC 14:35:16"

With Babashka (bb), I wish to have the least dependency. Otherwise, I could use clj-time, etc. to achieve the goal.

Or if I should just use the OS's date utility instead, given I'm writing a script?


Solution

  • In babashka you can use the java.time package:

    (import 'java.time.format.DateTimeFormatter
            'java.time.LocalDateTime)
    
    (def date (LocalDateTime/now))
    (def formatter (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
    (.format date formatter) ;;=> "2020-07-18 18:04:04"