This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # announcements (1)
- # babashka (9)
- # beginners (82)
- # calva (6)
- # cider (3)
- # clj-kondo (69)
- # cljdoc (4)
- # cljs-dev (10)
- # cljsrn (2)
- # clojure (74)
- # clojure-europe (11)
- # clojure-italy (9)
- # clojure-nl (15)
- # clojure-spec (18)
- # clojure-uk (89)
- # code-reviews (8)
- # core-async (42)
- # cursive (22)
- # datomic (26)
- # fulcro (13)
- # graalvm (31)
- # graphql (1)
- # leiningen (20)
- # malli (19)
- # music (1)
- # off-topic (4)
- # pathom (56)
- # re-frame (3)
- # reitit (26)
- # shadow-cljs (40)
- # spacemacs (5)
- # tools-deps (25)
it's a deep-south drawl written with a scandinavian alphabet 😬
what is a fada?
Just bringing it all back to Clojure for a moment... If one is using Java8 Date / Time instead of legacy or Joda, and one were to define a date, thus:
(jt/local-date 2013 2)
Does anyone know how to get the last day of the month from the resulting date object..?
because when you set year and month you default to first day of the month, but I would have thought I could extract the last day of the month from the object...
(java-time/minus (java-time/local-date 2013 3) (java-time/days 1))
would that be cheating?
@ben.hammond - no, that would not be cheating, I just had not thought of it!
Plus it's a bit long-winded, I was hoping for some syntactic sugar, but overall, this will get the job done, so thanks 🙂
public LocalDate withDayOfMonth(int dayOfMonth)
Of course it won't work for December, at least not as easily as I will have to take one day off the 1st of the January of the following year
java.time.LocalDate I note
Right, but I want the date object to return to me, programatically, the last day of the month in question. I know I could create the date with the last day, but I am looping over numbers and don't have a data structure of my own to manage the business rule(s) around the lengths of months, i.e. January has 31 days, but February has 28 except if the year is cleanly divisible by 4 in which case it has 29 days, etc.
So your solution above is great apart from the last trip around the loop as I can't create
(java-time/local-date 2013 13)
@dharrigan..? Please enlighten me, I am afraid I know not of what you speak
Thing to remember is everything we know about time is probably wrong.
@dharrigan - Thx, I had no clue about that 100 / 400 year rule - that's fascinating
@paulspencerwilliams - Thanks for that... I think 😉
Honestly, they can be a mare. A few systems ago we had a problem because an Indonesian island changed their boundaries switching it into another timezone, and until Java recognised it, all commodity trades there were an hour out.
The Timezone database is literally full of absurdities and quirks like this: https://blog.jonudell.net/2009/10/23/a-literary-appreciation-of-the-olsonzoneinfotz-database/
(import [java.time LocalDate]) (import [java.time.temporal TemporalAdjusters]) (def now (LocalDate/now)) (.with now (TemporalAdjusters/lastDayOfMonth))
user=> (tick.core/last-day-of-month (tick.alpha.api/new-date 2019 12)) #time/date "2019-12-31"
(-> (java-time/local-date 2013 12) (java-time/plus (java-time/months 1)) (java-time/minus (java-time/days 1)))
(defn get-last-day-of-month "Function to get valid last day of month from year and month" [year month] (let [year-modified (if (= 12 month) (+ 1 year) year) month-modified (if (= 12 month) 1 (+ 1 month))] (jt/as (jt/minus (jt/local-date year-modified month-modified) (jt/days 1)) :day-of-month)))
(java-time/adjust (java-time/local-date 2013 12) :last-day-of-month)
@dharrigan - Because I am using it via the Clojure wrapper and I don't want to do interop
(sort (keys (merge java-time.adjuster/base-adjusters java-time.adjuster/extra-adjusters))) => (:day-of-week-in-month :first-day-of-month :first-day-of-next-month :first-day-of-next-year :first-day-of-year :first-in-month :last-day-of-month :last-day-of-year :last-in-month :next-day-of-week :next-or-same-day-of-week :previous-day-of-week :previous-or-same-day-of-week)
but @ben.hammond has found the thing I was looking for, so even though I enjoyed my function ^^ I can make it a LOT nicer now...
I've started to really like
java-time since I was shown https://github.com/henryw374/time-literals
it doesn't (often) make me do things that I don't want to do
I gather the other nice Java Time wrapper is https://github.com/henryw374/cljc.java-time which also works in cljs.
(but at work we use https://github.com/dm3/clojure.java-time )
Haven't looked at tick yet. It's on my list 🙂
(my very long list!)
@seancorfield - if you are using clojure.java-time at work, maybe you can help me with something else... I need to get a UNIX timestamp out of a java-time/local-time, but I am struggling to find the docs... Is this something I can do..?
Funny you should mention that -- I was just working on some code yesterday that includes a
->unix-time function... let me look that up...
Do you mean
local-date-time? You need a date as well as a time to get to Unix time...
Pretty sure you need to get to an Instant or OffsetDateTime first, and then call
user=> (jt/local-date-time) #object[java.time.LocalDateTime 0x9353d68 "2019-11-28T10:26:30.324"] user=> (.toEpochSecond *1 java.time.ZoneOffset/UTC) 1574936790 user=>
That is super-sweet - shame there's no syntactic sugar to do it without interop, but still, very nice!
(-> (jt/instant) (jt/to-millis-from-epoch) (quot 1000))
(again, you need to go from a local date/time to an instant or offset date time for it to work)
user=> (-> (jt/offset-date-time) (jt/to-millis-from-epoch) (quot 1000)) 1574965982