Reputation: 518
I'm sitting in the America/Sao_Paulo (or some other random timezone) and now I want to get a LocalDate for the current date in Europe/London.
How can I achieve this?
(I've got a localDate that I know is in london, and I want to check if it's before the current date using clj-time.core/after?)
Upvotes: 1
Views: 1370
Reputation: 29958
The clj-time
library is based on the old Joda Time library. Both of these have been deprecated in favor of java.time
(available since JDK 8). It is best to use Java interop with java.time instead of the older clj-time
library.
The package java.time
is basically Joda Time 2.0, but rewritten to be cleaner and to correct some corner cases. Both were created by the same person.
In Java, a LocalDate
doesn't have a timezone. That is the whole point.
It is used for things like a birthday, such as 1999-12-31, where we don't consider your birthday to have a timezone.
For the same reason, the LocalDate
doesn't have any time associated with it, just as your birthday is considered to be the entire day.
See the java.time
docs for more information: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html
From the JavaDocs:
Dates and Times
Instant is essentially a numeric timestamp. The current Instant can be retrieved from a Clock. This is useful for logging and persistence of a point in time and has in the past been associated with storing the result from System.currentTimeMillis().
LocalDate stores a date without a time. This stores a date like '2010-12-03' and could be used to store a birthday.
LocalTime stores a time without a date. This stores a time like '11:30' and could be used to store an opening or closing time.
LocalDateTime stores a date and time. This stores a date-time like '2010-12-03T11:30'.
ZonedDateTime stores a date and time with a time-zone. This is useful if you want to perform accurate calculations of dates and times taking into account the ZoneId, such as 'Europe/Paris'. Where possible, it is recommended to use a simpler class without a time-zone. The widespread use of time-zones tends to add considerable complexity to an application.
Helper functions:
I have added some convenience functions to aid the usage of java.time
that you may find useful.
Upvotes: 2
Reputation: 338306
I do not know Clojure. So I'll use Java syntax which you can translate.
I'm sitting in the America/Sao_Paulo (or some other random timezone)
Irrelevant. Whether your tushy is in Tokyo, Reykjavík, or São Paulo, that has nothing to do with asking for the current date in Europe/London
time zone.
and now I want to get a LocalDate for the current date in Europe/London.
Call LocalDate.now
while passing the time zone of interest.
ZoneId zoneId = ZoneId.of( "Europe/London" ) ;
LocalDate currentDateInLondon = LocalDate.now( zoneId ) ;
I've got a localDate that I know is in london, and I want to check if it's before the current date
You can compare LocalDate
objects by calling isEqual
, isBefore
, and isAfter
.
LocalDate localDate = LocalDate.of( 2021 , Month.JANUARY , 23 ) ;
boolean isCurrentDateInLondonBeforeThatDate = currentDateInLondon.isBefore( localDate ) ;
Upvotes: 3
Reputation: 37008
There seems not to be a function to get the LocalDate
from
a DateTime
or alike in clj-time
-- but Joda Time has
.toLocalDate
. So e.g. you can do something like this:
(defn date-at-zone
[instant zone-id]
(.toLocalDate
(t/to-time-zone
instant
(t/time-zone-for-id zone-id))))
(let [instant (t/date-time 2020 12 12 1)
london-date (date-at-zone instant "Europe/London")
sao-paulo-date (date-at-zone instant "America/Sao_Paulo")]
(print (map str [instant london-date sao-paulo-date (t/after? london-date sao-paulo-date)])))
; → (2020-12-12T01:00:00.000Z 2020-12-12 2020-12-11 true)
(e.g. on 2020-12-12T01:00 Zulu it is still 2020-12-11 in Sao Paulo and therefor London "is strictly after" Sao Paulo when looking at just the date -- or it's already "tomorrow" in London)
Upvotes: 1