<commit_before>---
layout: tutorial
title: "Joda time module"
section: tutorial
sort_order: 10
---
[Joda-Time](http://www.joda.org/joda-time/) is a very well thought out date and time library for Java that happens to
be very popular in Scala - at the very least, it's quite a bit better than the stdlib [`Date`]. kantan.xpath provides
support for it through a dedicated module.

The `joda-time` module can be used by adding the following dependency to your `build.sbt`:

```scala
libraryDependencies += "com.nrinaudo" %% "kantan.xpath-joda-time" % "0.1.7"
```

You then need to import the corresponding package:

```tut:silent
import kantan.xpath.joda.time._
```

There are so many different ways of serialising dates that kantan.xpath doesn't have a default implementation - whatever
the choice, it would end up more often wrong than right.

 If you can provide a [`DateTimeFormat`] instance, however, you can easily get [`NodeDecoder`] instance for the
 following types:


* [`DateTime`]
* [`LocalDate`]
* [`LocalDateTime`]
* [`LocalTime`]

Let's imagine for example that we want to extract dates from the following string:

```tut:silent
import kantan.xpath.implicits._

val input = "<root><date value='12-10-1978'/><date value='09-01-2015'/></root>"
```

We'd simply need to declare the appropriate decoder through:

```tut:silent
import kantan.xpath.joda.time._
import org.joda.time.format._

implicit val decoder = localDateDecoder(DateTimeFormat.forPattern("DD-MM-yyyy"))
```

And we're done, as far as decoding is concerned. We only need to get a regular expression together and evaluate it:

```tut
input.evalXPath[List[org.joda.time.LocalDate]](xp"//date/@value")
```



[`Date`]:https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
[`DateTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html
[`LocalDate`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDate.html
[`LocalDateTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDateTime.html
[`LocalTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalTime.html
[`DateTimeFormat`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
[`NodeDecoder`]:{{ site.baseurl }}/api/kantan/xpath/NodeDecoder$.html<commit_msg>Update joda-time documentation to include default instances.<commit_after>---
layout: tutorial
title: "Joda time module"
section: tutorial
sort_order: 10
---
[Joda-Time](http://www.joda.org/joda-time/) is a very well thought out date and time library for Java that happens to
be very popular in Scala - at the very least, it's quite a bit better than the stdlib [`Date`]. kantan.xpath provides
support for it through a dedicated module.

The `joda-time` module can be used by adding the following dependency to your `build.sbt`:

```scala
libraryDependencies += "com.nrinaudo" %% "kantan.xpath-joda-time" % "0.1.7"
```

You then need to import the corresponding package:

```tut:silent
import kantan.xpath.joda.time._
```

kantan.xpath has default, ISO 8601 compliant [`NodeDecoder`] instances for the following types:

* [`DateTime`]
* [`LocalDate`]
* [`LocalDateTime`]
* [`LocalTime`]

Let's imagine for example that we want to extract dates from the following string:

```tut:silent
import kantan.xpath.implicits._

val input = "<root><date value='1978-10-12'/><date value='2015-01-09'/></root>"
```

This is directly supported:

```tut
input.evalXPath[List[org.joda.time.LocalDate]](xp"//date/@value")
```

It is, of course, possible to declare your own [`NodeDecoder`]. This is, for example, how you'd create a custom
[`NodeDecoder[LocalDate]`][`NodeDecoder`]:

```tut:silent
import org.joda.time.format._

val input = "<root><date value='12-10-1978'/><date value='09-01-2015'/></root>"

implicit val decoder = localDateDecoder(DateTimeFormat.forPattern("dd-MM-yyyy"))
```

And we're done, as far as decoding is concerned. We only need to get an XPath expression together and evaluate it:

```tut
input.evalXPath[List[org.joda.time.LocalDate]](xp"//date/@value")
```



[`Date`]:https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
[`DateTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html
[`LocalDate`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDate.html
[`LocalDateTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalDateTime.html
[`LocalTime`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/LocalTime.html
[`DateTimeFormat`]:http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
[`NodeDecoder`]:{{ site.baseurl }}/api/kantan/xpath/NodeDecoder$.html
{'fraction_non_alphanumeric': 0.15213638325420803, 'fraction_numerical': 0.013810962451445835, 'mean_word_length': 5.082677165354331, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4706768', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1530, 'n_words': 403}