java.util.Date Date someDate = new Date(1967, 5, 10); System.out.println(someDate); // What date is this? May 10th 1967 June 11th 1967 October 5th 1967 November 6th 1967
java.util.Date Date someDate = new Date(1967, 5, 10); System.out.println(someDate); Answer: June 10th 3867 Year: years from 1900 Month: 0-indexed Day: 1-indexed
Java 6 Date/Time API • Mutable • 0-indexed months • Little support for simple operations • No representation for duration, non-time-zone dates, only dates, only times etc.
JVM Alternatives • Joda Time • Since 2004 • De-facto date/time solution pre-Java 8 • JSR-310 / Java 8 API • Released in 2014 • Same project lead as Joda Time • Official API for Java 8, backported to Java 6
Joda time on Android • Can use directly, but large memory footprint • See problem description • Solution: https://github.com/dlew/joda-time-android • Method count: 5053
JSR-310 / Java 8 API • Official JVM solution (>= Java 8) • Built with experience from Joda time • Generally better performance • Smaller package, fewer methods
ThreeTen on Android • Backport library “ThreeTen”, Java 6 compatible • Same problem as Joda if use JVM lib directly • Use: https://github.com/JakeWharton/ThreeTenABP • Method count: 3278
Testing • UI tests — just test the app • Robolectric tests — initialise with Robo context • Plain JUnit tests — use JVM back port testCompile 'org.threeten:threetenbp: 1.3.1'
Testing • Note: Can’t mix Robolectric and plain JUnit tests • Separate plain JUnit and Robolectric tests • If using ThreeTenABP in a module/configuration, all tests (that involve ThreeTen) must be Robolectric
Any negatives? • Immutable (Android GC’ing is not JVM GC’ing) • 5% towards DEX limit • Need to connect old and new APIs • Android specific locale issues (e.g. 24-hr toggle)