clojuretimezoneclj-time

clj-time - how to get local-date from curent time in a different timezone


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


Solution

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