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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Alex Florescu
March 12, 2016
Programming
220
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
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
930
How we've built Yahoo Fantasy Football (Droidcon Italy '15)
anothem
1
780
Other Decks in Programming
See All in Programming
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
870
高専、大学編入、そして未踏へ〜プロダクト開発とキャリアの歩み - Technical College, University Transfer, and On to “Mitou” / My Journey in Product Development and Career
pkmiya
0
130
【デモ】Kiroで体験する仕様駆動開発|設計からコーディングまでAIと進める開発フロー
cmkudo
0
540
Claude Code全社展開のためにやったことn選~プラグイン302個・コミッター271人を支えるために~
kenchan
5
1.8k
[DroidKaigi 26] How We Moved from Android Studio to Slack
thisaay
0
150
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
310
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
150
MySQLとPostgreSQLって何が違うの?
akagami
0
150
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
600
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
480
いまどきの Codex で開発する visionOS アプリの開発スタイルについて
karad
0
180
信頼性の目標を誰も求めてない
shubox
0
470
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
58k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Product Roadmaps are Hard
iamctodd
55
13k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
490
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
Marketing to machines
jonoalderson
1
5.7k
We Are The Robots
honzajavorek
0
330
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
360
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