Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Modern Date/Time APIs on Android
Search
Alex Florescu
March 12, 2016
Programming
0
150
Modern Date/Time APIs on Android
Droidcon Bucharest 2016
Alex Florescu
March 12, 2016
Tweet
Share
More Decks by Alex Florescu
See All by Alex Florescu
Java 8 on Android
anothem
2
330
How to talk to your users
anothem
1
840
How we've built Yahoo Fantasy Football (Droidcon Italy '15)
anothem
1
690
Other Decks in Programming
See All in Programming
S3静的ホスティング+Next.js静的エクスポート で格安webアプリ構築
iharuoru
0
200
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.3k
AIエージェントを活用したアプリ開発手法の模索
kumamotone
1
770
リアルタイムレイトレーシング + ニューラルレンダリング簡単紹介 / Real-Time Ray Tracing & Neural Rendering: A Quick Introduction (2025)
shocker_0x15
1
240
Agentic Applications with Symfony
el_stoffel
1
150
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
13
4k
Go1.24で testing.B.Loopが爆誕
kuro_kurorrr
0
170
Defying Front-End Inertia: Inertia.js on Rails
skryukov
0
350
AHC 044 混合整数計画ソルバー解法
kiri8128
0
310
AIコードエディタの基盤となるLLMのFlutter性能評価
alquist4121
0
170
AIコーディングワークフローの試行 〜AIエージェント×ワークフローでの自動化を目指して〜
rkaga
0
250
SideKiqでジョブが二重起動した事象を深堀りしました
t_hatachi
0
260
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
39
7.2k
Optimizing for Happiness
mojombo
377
70k
Unsuck your backbone
ammeep
670
57k
Docker and Python
trallard
44
3.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
How GitHub (no longer) Works
holman
314
140k
A Tale of Four Properties
chriscoyier
158
23k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
BBQ
matthewcrist
88
9.5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
17
1.1k
Transcript
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