Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Modern Date/Time APIs on Android
Search
Alex Florescu
March 12, 2016
Programming
240
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Modern Date/Time APIs on Android
Droidcon Bucharest 2016
Alex Florescu
March 12, 2016
Transcript
26 slides
Modern Date/Time APIs on Android Droidcon Bucharest 2016 Alex Florescu
@flor3scu YPlan
Overview • Why • Joda time and JSR-310 • What
can you do with them • Setup & testing • Good and bad
Date and Time is hard • Timezones • Daylight savings
• Different calendar systems • Leap days/seconds/etc • AND MORE
Quiz: Leap years • A leap year (e.g. 2016) adds
a day, February 29th • What years are leap years?
Quiz: What is a leap year? • Divisible by four
Quiz: What is a leap year? • Divisible by four
• But NOT divisible by 100
Quiz: What is a leap year? • Divisible by four
• But NOT divisible by 100 • UNLESS divisible by 400
What’s wrong with Java 6 Date?
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 • Still actively maintained • For new projects
use Java 8 API • See comparison and Stephen Colebourne’s blog
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
Setup • In build.gradle: compile 'com.jakewharton.threetenabp:threetenabp:1.0.3' • In Application.onCreate(): @Override
public void onCreate() { super.onCreate(); AndroidThreeTen.init(this); }
Cool things ZonedDateTime.now(); //2016-03-12T12:11:25.371+02:00 [Europe/Bucharest] ZonedDateTime.now(ZoneId.of("Europe/ London")); //2016-03-12T10:11:26.374+00:00 [Europe/London]
Cool things LocalDateTime now = LocalDateTime.now(); LocalDate tomorrowDay = now.toLocalDate().plusDays(1);
LocalDateTime noonTomorrow = tomorrowDay.atTime(12, 0);
Cool things LocalTime now = LocalTime.now(); LocalTime lunchTime =
LocalTime.of(12, 30); if (now.isAfter(lunchTime)) { // GO TO LUNCH }
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
Why should you use? • Safer, clearer, fluent API •
Easier to do everything • Immutable • Excellent timezone support
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)
More docs • ThreeTen Reference • JavaDoc
Thank you • Slides: http://bit.do/AlexDroidconRo • T: @flor3scu
More Decks by Alex Florescu
See All by Alex Florescu
Java 8 on Android
anothem
2
390
How to talk to your users
anothem
1
950
How we've built Yahoo Fantasy Football (Droidcon Italy '15)
anothem
1
780
Other Decks in Programming
See All in Programming
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
150
Debian JP Projectのサーバー構成
uwabami
1
180
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
220
Crash Auto Pipelines
veronikapj
1
450
ローカルLLMの基本と近況 2611 / Local LLM basics and recent situation 2611
kishida
3
660
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
400
数行の変更でDockerビルドを最大91%高速化~GitLab CIの実行基盤を社内40リポジトリに展開するまで~
picopico
3
810
Kotlin Fest 2026: Kotlin コンパイラに Contribute してみよう! - Kotlin/Wasm で深い再帰を書けるようにした話
ternbusty
1
420
Going Full Circle with Virtual Graphs and Relational Databases
michaelsimons
1
110
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
950
Featured
See All Featured
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
1
320
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
6
970
Music & Morning Musume
bryan
48
7.5k
エンジニアに許された特別な時間の終わり
watany
109
250k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7.1k
How to build a perfect <img>
jonoalderson
1
6.3k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
Raft: Consensus for Rubyists
vanstee
143
7.9k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
630
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
860
Build your cross-platform service in a week with App Engine
jlugia
234
19k