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
140
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
310
How to talk to your users
anothem
1
760
How we've built Yahoo Fantasy Football (Droidcon Italy '15)
anothem
1
650
Other Decks in Programming
See All in Programming
デザインシステムで Tailwind CSSとCSS in JSに分散投資をしたら良かった話
fsubal
18
4.8k
1인 개발자로 행복하게 살기 - GDG 송도 헬로월드 2024
benjaminkim
1
5.6k
コードレビューで学ぶ!Kotlinオブジェクト指向デザインパターン
akkie76
2
180
雑に思考を整理する技術と効能
konifar
55
25k
try! Swift Tokyo 初参加報告LT
hinakko2
0
190
Elm 0.19.0 Changes
bkuhlmann
0
480
Rails と人魚の話/rails-and-mermaid
sanfrecce_osaka
0
100
Build with AI 2024 Seoul - 제로부터 시작하는 Flutter with Gemini 생활 - 박제창
itsmedreamwalker
0
200
せっかくモデル図描くのなら、嬉しいことが多い方がいいよね!
kuboaki
1
3.1k
Folding Cheat Sheet #3
philipschwarz
PRO
0
110
脱・初心者!脱・マネコン!AWS CDKを使ってみませんか!?
har1101
0
300
Elm Form Validation
bkuhlmann
0
500
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
50
8.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
The Language of Interfaces
destraynor
151
23k
The Cost Of JavaScript in 2023
addyosmani
14
3.8k
A Tale of Four Properties
chriscoyier
150
22k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
1
3.4k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
186
16k
Designing for Performance
lara
601
67k
10 Git Anti Patterns You Should be Aware of
lemiorhan
646
57k
Designing Experiences People Love
moore
136
23k
What's in a price? How to price your products and services
michaelherold
237
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
24
2.3k
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