How make implicit Ordered on java.time.LocalDate
Asked Answered
H

6

22

I want to use java.time.LocalDate and java.time.LocalDateTime with an implicit Ordered like:

val date1 = java.time.LocalDate.of(2000, 1, 1)
val date2 = java.time.LocalDate.of(2010, 10, 10)
if (date1 < date2) ...

import scala.math.Ordering.Implicits._ doesn't work, because LocalDate inherits from Comparable<ChronoLocalDate> instead of Comparable<LocalDate>. How can I write my own imlicit Orderd to use <, <=, >, >= operators/methods to compare LocalDate's?

Edit:

I found a way with use of an implicit class:

import java.time.{LocalDate}

object MyDateTimeUtils {
  implicit class MyLocalDateImprovements(val ld: LocalDate)
           extends Ordered[LocalDate] {
    def compare(that: LocalDate): Int = ld.compareTo(that)
  }
}

// Test

import MyDateTimeUtils._

val d1 = LocalDate.of(2016, 1, 1)
val d2 = LocalDate.of(2017, 2, 3)

if (d1 < d2) println("d1 is less than d2")

But I would prefer a way like Scala is doing for all Java classes which implements Comparable<T>. You just have to import scala.math.Ordering.Implicits._ in your code. Scala implements it like:

implicit def ordered[A <% Comparable[A]]: Ordering[A] = new Ordering[A] {
  def compare(x: A, y: A): Int = x compareTo y
}

But unfortunately LocalDate implements Comparable<ChronoLocalDate> instead of Comparable<LocalDate>. I couldn't find a way to modify the above implicit ordered method to fit with LocalDate/Comparable<ChronoLocalDate>. Any idea?

Hypoglossal answered 27/6, 2016 at 16:46 Comment(2)
Thank you for pointing our that import scala.math.Ordering.Implicits._ doesn't work, because LocalDate inherits from Comparable<ChronoLocalDate> instead of Comparable<LocalDate> - I'd been wondering for ages why that wasn't working!Asymmetric
Since Scala 2.13.0 this just works without having to define your own Ordering.Melaniamelanic
G
28

You can use Ordering.by to create ordering for any type, given a function from that type to something that already has an Ordering - in this case, to Long:

implicit val localDateOrdering: Ordering[LocalDate] = Ordering.by(_.toEpochDay)
Gabon answered 27/6, 2016 at 17:11 Comment(2)
Thank's, but I want to use expressions like if (date1 < date2) in my programm code. With java.time.Instant for example it's possible when I import scala.math.Ordering.Implicits._, because Instant inherits "Comparable<Instant>, but LocalDate only Comparable<ChronoLocalDate> instead of Comparable<LocalDate>.Hypoglossal
also it seems toEpochDay is no longer availablePutamen
A
26

Comparing by LocalDate.toEpochDay is clear, though maybe relatively slow...

The answer by @tzach-zohar is great, in that it's most obvious what is going on; you're ordering by Epoch Day:

implicit val localDateOrdering: Ordering[LocalDate] = Ordering.by(_.toEpochDay)

However, if you look at the implementation of toEpochDay you'll see that it's relatively involved - 18 lines of code, with 4 divisions, 3 conditionals and a call to isLeapYear() - and the resulting value isn't cached, so it gets recalculated with each comparison, which might be expensive if there were a large number of LocalDates to be sorted.

...making use of LocalDate.compareTo is probably more performant...

The implementation of LocalDate.compareTo is simpler - just 2 conditionals, no division - and it's what you'd be getting with that implicit conversion of java.lang.Comparable to scala.math.Ordering that scala.math.Ordering.Implicits._ offers, if only it worked! But as you said, it doesn't, because LocalDate inherits from Comparable<ChronoLocalDate> instead of Comparable<LocalDate>. One way to take advantage of it might be...

import scala.math.Ordering.Implicits._

implicit val localDateOrdering: Ordering[LocalDate] =
  Ordering.by(identity[ChronoLocalDate])

...which lets you order LocalDates by casting them to ChronoLocalDates, and using the Ordering[ChronoLocalDate] that scala.math.Ordering.Implicits._ gives you!

...and in the end it looks best with the lambda syntax for SAM types

The lambda syntax for SAM types, introduced with Scala 2.12, can make really short work of constructing a new Ordering :

implicit val localDateOrdering: Ordering[LocalDate] = _ compareTo _

...and I think this ends up being my personal favourite! Concise, still fairly clear, and using (I think) the best-performing comparison method.

Asymmetric answered 17/5, 2018 at 15:56 Comment(0)
M
5

A slight modification to the implicit ordered should do the trick.

type AsComparable[A] = A => Comparable[_ >: A]

implicit def ordered[A: AsComparable]: Ordering[A] = new Ordering[A] {
  def compare(x: A, y: A): Int = x compareTo y
}

Now every type which is comparable to itself or to a supertype of itself should have an Ordering.

Melaniamelanic answered 18/10, 2017 at 13:41 Comment(4)
Note that view-bounds (<%) were deprecated in Scala 2.11, so you might get complaints from the compiler about using them. issues.scala-lang.org/browse/SI-7629Asymmetric
@RobertoTyley You're right. I updated my solution for a world without view bounds.Melaniamelanic
I think this is the most useful answer. It addresses both LocalDate and LocalDateTime in one fell swoop, and also sidesteps the performance considerations of toEpochDay, and also solves the problems of using only toEpochSecond for comparing timestamps that go down to nanoseconds.Akan
For the future reader: this modified definition is now part of the standard library since 2.13.0, so this all just works now.Melaniamelanic
P
2

Here is the solution that I use:

define two implicits. The first one for making an Ordering[LocalDate] available. And a second one for giving LocalDate a compare method which comes in very handy. I typically put these in package objects in a library I can just include where I need them.

package object net.fosdal.oslo.odatetime {

  implicit val orderingLocalDate: Ordering[LocalDate] = Ordering.by(d => (d.getYear, d.getDayOfYear))

  implicit class LocalDateOps(private val localDate: LocalDate) extends AnyVal with Ordered[LocalDate] {
    override def compare(that: LocalDate): Int = Ordering[LocalDate].compare(localDate, that)
  }
}

with both of these defined you can now do things like:

import net.fosdal.oslo.odatetime._

val bool: Boolean = localDate1 < localDate1

val localDates: Seq[LocalDate] = ...
val sortedSeq = localDates.sorted

Alternatively... you could just use my library (v0.4.3) directly. see: https://github.com/sfosdal/oslo

Putamen answered 1/3, 2018 at 0:33 Comment(0)
C
2

Here's my solution for java.time.LocalDateTime

implicit val localDateTimeOrdering: Ordering[LocalDateTime] =
  Ordering.by(x => x.atZone(ZoneId.of("UTC")).toEpochSecond)
Charie answered 11/3, 2019 at 6:38 Comment(0)
M
0

one possible implementation in Scala 3 is to use a Conversion:

  given toOrderedLocalDate: Conversion[LocalDate, Ordered[LocalDate]] with
    def apply(x: LocalDate): Ordered[LocalDate] = (y: LocalDate) => x.compareTo(y)

Mica answered 1/7, 2022 at 15:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.