Skip to content

Time: Use java.time rather than Joda-Time #18

@rtyley

Description

@rtyley

Representing time - moments in time, durations, calendar calculations, etc - should always be done with strongly-typed classes where possible, rather than simple integer types (which are a bad choice because they don't include units - eg. 5 could represent 5 minutes, seconds, or even milliseconds).

Joda-Time recommends moving to java.time.* - and has for a long time!

Java has had an excellent built-in time-representation library in the java.time.* package since Java 8, released in 2014. Prior to that, the best available option was Joda-Time (written by the same person who went on to advise on java.time, Stephen Colebourne) - and at the Guardian we still have active production code that still uses Joda-Time - but where we're updating code, we should aim to migrate to java.time.*.

Equivalent classes in java.time

  • org.joda.time.Instantjava.time.Instant
  • org.joda.time.DateTimejava.time.ZonedDateTime - assuming that you need to have localised date - if not, you may want to be using java.time.Instant instead.

See https://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html for a full list.

Examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions