SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
This thing all things devours; 
Birds, beasts, trees, flowers; 
Gnaws iron, bites steel; 
Grinds hard stones to meal; 
Slays king*, ruins town, 
And beats mountain down. 
J.R.R. Tolkien 
* and software developer alike
 2010-08-03T03:30Z 
a. sunrise 
b. sunset 
c. could be either 
d. that's no sun!
 2010-08-03T03:30Z 
a. sunrise 
b. sunset 
c. could be either 
d. that's no sun!
 2010-08-03T03:30Z  49.788, -122.987 
a. resolve using nearest city 
b. resolve using tz_world shapefile 
c. resolve with Google Maps API 
d. store the time zone offset
 2010-08-03T03:30Z  49.788, -122.987 
a. resolve using nearest city 
b. resolve using tz_world shapefile 
c. resolve with Google Maps API 
d. store the time zone offset
 2010-08-03T03:30Z  49.788, -122.987 
a. resolve using nearest city 
b. resolve using tz_world shapefile 
c. resolve with Google Maps API 
d. store the time zone offset
 2010-08-03T03:30Z  49.788, -122.987 
a. resolve using nearest city 
b. resolve using tz_world shapefile 
c. resolve with Google Maps API 
d. store the time zone offset
import org.joda.time.DateTime; 
DateTime time = new DateTime("2010-08-02T20:30:00-07:00"); 
assertEquals(20, time.getHourOfDay()); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
DateTime time = new DateTime("2010-08-02T20:30:00-07:00"); 
assertEquals(20, time.getHourOfDay()); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
DateTimeZone zone = DateTimeZone.forOffsetHours(-7); 
DateTime time = new DateTime("2010-08-02T20:30:00-07:00", zone); 
assertEquals(20, time.getHourOfDay()); 
Solution #1 
Pass an explicit zone to the constructor.
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.format.DateTimeFormatter; 
import org.joda.time.format.ISODateTimeFormat; 
DateTimeFormatter fmt = ISODateTimeFormat 
.dateTimeNoMillis().withOffsetParsed(); 
DateTime time = DateTime.parse("2010-08-02T20:30:00-07:00", fmt); 
assertEquals(20, time.getHourOfDay()); 
Solution #2 
Use a custom formatter.
import java.time.OffsetDateTime; 
OffsetDateTime time = 
OffsetDateTime.parse("2010-08-02T20:30:00-07:00"); 
assertEquals(20, time.getHourOfDay()); 
Solution #3 
Use java.time.OffsetDateTime
 9 hours 
 2014-03-08T20:00-08:00 
a. 5am 
b. 6am 
c. too early! 
d. it depends 
When did I get up?
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.Duration; 
DateTimeZone zone = DateTimeZone.forOffsetHours(-8); 
DateTime time = new DateTime("2014-03-08T20:00:00", zone); 
Duration d = Duration.standardHours(9); 
assertEquals(6, time.plus(d).getHourOfDay()); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.Duration; 
DateTimeZone zone = DateTimeZone.forOffsetHours(-8); 
DateTime time = new DateTime("2014-03-08T20:00:00", zone); 
Duration d = Duration.standardHours(9); 
assertEquals(6, time.plus(d).getHourOfDay()); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.Duration; 
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); 
DateTime time = new DateTime("2014-03-08T20:00:00", zone); 
Duration d = Duration.standardHours(9); 
assertEquals(6, time.plus(d).getHourOfDay()); 
Don't do math without time zone IDs!
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.Duration; 
import org.joda.time.Period; 
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); 
DateTime time = new DateTime("2014-03-08T20:00:00", zone); 
Duration d = Duration.standardDays(1); 
Period p = Period.days(1); 
assertEquals(time.plus(d), time.plus(p)); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
import org.joda.time.DateTimeZone; 
import org.joda.time.Duration; 
import org.joda.time.Period; 
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); 
DateTime time = new DateTime("2014-03-08T20:00:00", zone); 
Duration d = Duration.standardDays(1); 
Period p = Period.days(1); 
assertEquals(time.plus(d), time.plus(p)); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import java.time.DateTime; 
import java.time.DateTimeZone; 
import java.time.Duration; 
import java.time.Period; 
ZonedDateTime time = ZonedDateTime 
.parse("2014-03-08T20:00:00-08:00[America/Los_Angeles]"); 
Duration d = Duration.ofDays(1); 
Period p = Period.ofDays(1); 
assertEquals(time.plus(d), time.plus(p)); 
a. passes 
b. fails 
c. throws an exception 
d. it depends
import org.joda.time.DateTime; 
import org.joda.time.Duration; 
DateTime begin = new DateTime(); 
// watch me run 100 m 
DateTime end = new DateTime(); 
Duration d = new Duration(begin, end); 
assertFalse(d.isShorterThan(Duration.millis(9580))); 
a. never fails 
b. usually fails 
c. always fails 
d. TimeOutException
import org.joda.time.DateTime; 
import org.joda.time.Duration; 
DateTime begin = new DateTime(); 
// watch me run 100 m 
DateTime end = new DateTime(); 
Duration d = new Duration(begin, end); 
assertFalse(d.isShorterThan(Duration.millis(9580))); 
a. never fails 
b. usually fails 
c. always fails 
d. TimeOutException
import java.util.concurrent.TimeUnit; 
import org.joda.time.Duration; 
long begin = System.nanoTime(); 
// watch me run 100 m 
long end = System.nanoTime(); 
Duration d = Duration.millis( 
TimeUnit.NANOSECONDS.toMillis(end – begin)); 
assertFalse(d.isShorterThan(Duration.millis(9580))); 
Don't rely on System.currentTimeMillis(), 
and store an explicit duration.
import org.joda.time.*; 
import org.joda.time.Interval; 
import org.joda.time.LocalDate; 
int steps = 6000; 
LocalDate date = LocalDate.parse("2014-11-02"); 
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); 
Interval interval = date.toInterval(zone); 
long hours = interval.toDuration().getStandardHours(); 
assertEquals(240, steps / hours); 
a. passes 
b. fails 
c. exception 
d. it depends
import org.joda.time.*; 
import org.joda.time.Interval; 
import org.joda.time.LocalDate; 
int steps = 6000; 
LocalDate date = LocalDate.parse("2014-11-02"); 
DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); 
Interval interval = date.toInterval(zone); 
long hours = interval.toDuration().getStandardHours(); 
assertEquals(240, steps / hours); 
a. passes 
b. fails 
c. exception 
d. it depends
Need to store 
a timestamp 
Is it a range? 
no 
Does 
local time 
matter? 
Elapsed 
time must be 
accurate? 
yes yes 
Need to 
add or subtract 
time? 
Store duration 
no no 
Store with 
zone offset 
Store with 
zone ID 
Store as UTC 
no 
yes yes 
for begin and end 
Store interval
Illustration by John Tenniel, Alice in Wonderland 
Start at the beginning, 
and go on til you come to the end: 
then stop.

Más contenido relacionado

La actualidad más candente

openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollideropenFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
Atsushi Tadokoro
 
What they don't tell you about JavaScript
What they don't tell you about JavaScriptWhat they don't tell you about JavaScript
What they don't tell you about JavaScript
Raphael Cruzeiro
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
akaptur
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
ecomputernotes
 

La actualidad más candente (20)

Aa
AaAa
Aa
 
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollideropenFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
 
What they don't tell you about JavaScript
What they don't tell you about JavaScriptWhat they don't tell you about JavaScript
What they don't tell you about JavaScript
 
Debugging TV Frame 0x0D
Debugging TV Frame 0x0DDebugging TV Frame 0x0D
Debugging TV Frame 0x0D
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
Class 24: Imperative Programming
Class 24: Imperative ProgrammingClass 24: Imperative Programming
Class 24: Imperative Programming
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
PyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for TornadoPyCon lightning talk on my Toro module for Tornado
PyCon lightning talk on my Toro module for Tornado
 
Weka วิเคราะห์ 55102011029
Weka วิเคราะห์ 55102011029Weka วิเคราะห์ 55102011029
Weka วิเคราะห์ 55102011029
 
JavaScript @ CTK
JavaScript @ CTKJavaScript @ CTK
JavaScript @ CTK
 
Query History of a Software Project
Query History of a Software ProjectQuery History of a Software Project
Query History of a Software Project
 
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines Reloaded
 
Python opcodes
Python opcodesPython opcodes
Python opcodes
 
Cilk Plus Parallel Reduction
Cilk Plus Parallel ReductionCilk Plus Parallel Reduction
Cilk Plus Parallel Reduction
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
Snake.c
Snake.cSnake.c
Snake.c
 
Time brings all things to pass
Time brings all things to passTime brings all things to pass
Time brings all things to pass
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is coming
 

Destacado

CAA Portfolio
CAA PortfolioCAA Portfolio
CAA Portfolio
beast
 

Destacado (12)

OSGi and Eclipse RCP
OSGi and Eclipse RCPOSGi and Eclipse RCP
OSGi and Eclipse RCP
 
Speed is Essential for a Great Web Experience (oredev)
Speed is Essential for a Great Web Experience (oredev)Speed is Essential for a Great Web Experience (oredev)
Speed is Essential for a Great Web Experience (oredev)
 
CAA Portfolio
CAA PortfolioCAA Portfolio
CAA Portfolio
 
Combining your data with Zenobase
Combining your data with ZenobaseCombining your data with Zenobase
Combining your data with Zenobase
 
Schneider Associates Launch of the Week: Snap Audience Match
Schneider Associates Launch of the Week: Snap Audience MatchSchneider Associates Launch of the Week: Snap Audience Match
Schneider Associates Launch of the Week: Snap Audience Match
 
Schneider Associates Launch of the Week: Facebook Slideshow Update
Schneider Associates Launch of the Week: Facebook Slideshow UpdateSchneider Associates Launch of the Week: Facebook Slideshow Update
Schneider Associates Launch of the Week: Facebook Slideshow Update
 
Speed is Essential for a Great Web Experience (Canvas Conf Version)
Speed is Essential for a Great Web Experience (Canvas Conf Version)Speed is Essential for a Great Web Experience (Canvas Conf Version)
Speed is Essential for a Great Web Experience (Canvas Conf Version)
 
Schneider Associates Launch of the Week: United Nations Social Media App SDGs...
Schneider Associates Launch of the Week: United Nations Social Media App SDGs...Schneider Associates Launch of the Week: United Nations Social Media App SDGs...
Schneider Associates Launch of the Week: United Nations Social Media App SDGs...
 
Schneider Associates Launch of the Week: Facebook Marketplace
Schneider Associates Launch of the Week: Facebook MarketplaceSchneider Associates Launch of the Week: Facebook Marketplace
Schneider Associates Launch of the Week: Facebook Marketplace
 
Faster Frontends
Faster FrontendsFaster Frontends
Faster Frontends
 
Plan De atención individualizado Grupo Los Nogales
Plan De atención individualizado Grupo Los Nogales Plan De atención individualizado Grupo Los Nogales
Plan De atención individualizado Grupo Los Nogales
 
Dynamics eg260 l1
Dynamics eg260 l1Dynamics eg260 l1
Dynamics eg260 l1
 

Similar a Java Time Puzzlers

Military time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdfMilitary time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdf
marketing413921
 
Codestrong 2012 breakout session hacking titanium
Codestrong 2012 breakout session   hacking titaniumCodestrong 2012 breakout session   hacking titanium
Codestrong 2012 breakout session hacking titanium
Axway Appcelerator
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
RonnBlack
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
adhityalapcare
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdf
anfenterprises
 
e computer notes - Date time functions
e computer notes - Date time functionse computer notes - Date time functions
e computer notes - Date time functions
ecomputernotes
 

Similar a Java Time Puzzlers (20)

Clock For My
Clock For MyClock For My
Clock For My
 
Environmental analysis of crop trials - Van Etten
Environmental analysis of crop trials - Van EttenEnvironmental analysis of crop trials - Van Etten
Environmental analysis of crop trials - Van Etten
 
rules, events and workflow
rules, events and workflowrules, events and workflow
rules, events and workflow
 
JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
 
Military time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdfMilitary time and Standard time, JavaOne of the assignments given .pdf
Military time and Standard time, JavaOne of the assignments given .pdf
 
Codestrong 2012 breakout session hacking titanium
Codestrong 2012 breakout session   hacking titaniumCodestrong 2012 breakout session   hacking titanium
Codestrong 2012 breakout session hacking titanium
 
Groovy
GroovyGroovy
Groovy
 
start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()start_printf: dev/ic/com.c comstart()
start_printf: dev/ic/com.c comstart()
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
 
jkfdlsajfklafj
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
 
doc
docdoc
doc
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Computer Science Presentation for various MATLAB toolboxes
Computer Science Presentation for various MATLAB toolboxesComputer Science Presentation for various MATLAB toolboxes
Computer Science Presentation for various MATLAB toolboxes
 
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
 
C vs Java: Finding Prime Numbers
C vs Java: Finding Prime NumbersC vs Java: Finding Prime Numbers
C vs Java: Finding Prime Numbers
 
Introduction to Date and Time API 4
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
 
Node.js flow control
Node.js flow controlNode.js flow control
Node.js flow control
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdf
 
PyParis - weather and climate data
PyParis - weather and climate dataPyParis - weather and climate data
PyParis - weather and climate data
 
e computer notes - Date time functions
e computer notes - Date time functionse computer notes - Date time functions
e computer notes - Date time functions
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 

Java Time Puzzlers

  • 1. This thing all things devours; Birds, beasts, trees, flowers; Gnaws iron, bites steel; Grinds hard stones to meal; Slays king*, ruins town, And beats mountain down. J.R.R. Tolkien * and software developer alike
  • 2.  2010-08-03T03:30Z a. sunrise b. sunset c. could be either d. that's no sun!
  • 3.  2010-08-03T03:30Z a. sunrise b. sunset c. could be either d. that's no sun!
  • 4.  2010-08-03T03:30Z  49.788, -122.987 a. resolve using nearest city b. resolve using tz_world shapefile c. resolve with Google Maps API d. store the time zone offset
  • 5.  2010-08-03T03:30Z  49.788, -122.987 a. resolve using nearest city b. resolve using tz_world shapefile c. resolve with Google Maps API d. store the time zone offset
  • 6.  2010-08-03T03:30Z  49.788, -122.987 a. resolve using nearest city b. resolve using tz_world shapefile c. resolve with Google Maps API d. store the time zone offset
  • 7.  2010-08-03T03:30Z  49.788, -122.987 a. resolve using nearest city b. resolve using tz_world shapefile c. resolve with Google Maps API d. store the time zone offset
  • 8. import org.joda.time.DateTime; DateTime time = new DateTime("2010-08-02T20:30:00-07:00"); assertEquals(20, time.getHourOfDay()); a. passes b. fails c. throws an exception d. it depends
  • 9. import org.joda.time.DateTime; DateTime time = new DateTime("2010-08-02T20:30:00-07:00"); assertEquals(20, time.getHourOfDay()); a. passes b. fails c. throws an exception d. it depends
  • 10. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; DateTimeZone zone = DateTimeZone.forOffsetHours(-7); DateTime time = new DateTime("2010-08-02T20:30:00-07:00", zone); assertEquals(20, time.getHourOfDay()); Solution #1 Pass an explicit zone to the constructor.
  • 11. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.format.DateTimeFormatter; import org.joda.time.format.ISODateTimeFormat; DateTimeFormatter fmt = ISODateTimeFormat .dateTimeNoMillis().withOffsetParsed(); DateTime time = DateTime.parse("2010-08-02T20:30:00-07:00", fmt); assertEquals(20, time.getHourOfDay()); Solution #2 Use a custom formatter.
  • 12. import java.time.OffsetDateTime; OffsetDateTime time = OffsetDateTime.parse("2010-08-02T20:30:00-07:00"); assertEquals(20, time.getHourOfDay()); Solution #3 Use java.time.OffsetDateTime
  • 13.  9 hours  2014-03-08T20:00-08:00 a. 5am b. 6am c. too early! d. it depends When did I get up?
  • 14. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; DateTimeZone zone = DateTimeZone.forOffsetHours(-8); DateTime time = new DateTime("2014-03-08T20:00:00", zone); Duration d = Duration.standardHours(9); assertEquals(6, time.plus(d).getHourOfDay()); a. passes b. fails c. throws an exception d. it depends
  • 15. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; DateTimeZone zone = DateTimeZone.forOffsetHours(-8); DateTime time = new DateTime("2014-03-08T20:00:00", zone); Duration d = Duration.standardHours(9); assertEquals(6, time.plus(d).getHourOfDay()); a. passes b. fails c. throws an exception d. it depends
  • 16. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); DateTime time = new DateTime("2014-03-08T20:00:00", zone); Duration d = Duration.standardHours(9); assertEquals(6, time.plus(d).getHourOfDay()); Don't do math without time zone IDs!
  • 17. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; import org.joda.time.Period; DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); DateTime time = new DateTime("2014-03-08T20:00:00", zone); Duration d = Duration.standardDays(1); Period p = Period.days(1); assertEquals(time.plus(d), time.plus(p)); a. passes b. fails c. throws an exception d. it depends
  • 18. import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; import org.joda.time.Period; DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); DateTime time = new DateTime("2014-03-08T20:00:00", zone); Duration d = Duration.standardDays(1); Period p = Period.days(1); assertEquals(time.plus(d), time.plus(p)); a. passes b. fails c. throws an exception d. it depends
  • 19. import java.time.DateTime; import java.time.DateTimeZone; import java.time.Duration; import java.time.Period; ZonedDateTime time = ZonedDateTime .parse("2014-03-08T20:00:00-08:00[America/Los_Angeles]"); Duration d = Duration.ofDays(1); Period p = Period.ofDays(1); assertEquals(time.plus(d), time.plus(p)); a. passes b. fails c. throws an exception d. it depends
  • 20. import org.joda.time.DateTime; import org.joda.time.Duration; DateTime begin = new DateTime(); // watch me run 100 m DateTime end = new DateTime(); Duration d = new Duration(begin, end); assertFalse(d.isShorterThan(Duration.millis(9580))); a. never fails b. usually fails c. always fails d. TimeOutException
  • 21. import org.joda.time.DateTime; import org.joda.time.Duration; DateTime begin = new DateTime(); // watch me run 100 m DateTime end = new DateTime(); Duration d = new Duration(begin, end); assertFalse(d.isShorterThan(Duration.millis(9580))); a. never fails b. usually fails c. always fails d. TimeOutException
  • 22. import java.util.concurrent.TimeUnit; import org.joda.time.Duration; long begin = System.nanoTime(); // watch me run 100 m long end = System.nanoTime(); Duration d = Duration.millis( TimeUnit.NANOSECONDS.toMillis(end – begin)); assertFalse(d.isShorterThan(Duration.millis(9580))); Don't rely on System.currentTimeMillis(), and store an explicit duration.
  • 23. import org.joda.time.*; import org.joda.time.Interval; import org.joda.time.LocalDate; int steps = 6000; LocalDate date = LocalDate.parse("2014-11-02"); DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); Interval interval = date.toInterval(zone); long hours = interval.toDuration().getStandardHours(); assertEquals(240, steps / hours); a. passes b. fails c. exception d. it depends
  • 24. import org.joda.time.*; import org.joda.time.Interval; import org.joda.time.LocalDate; int steps = 6000; LocalDate date = LocalDate.parse("2014-11-02"); DateTimeZone zone = DateTimeZone.forID("America/Los_Angeles"); Interval interval = date.toInterval(zone); long hours = interval.toDuration().getStandardHours(); assertEquals(240, steps / hours); a. passes b. fails c. exception d. it depends
  • 25. Need to store a timestamp Is it a range? no Does local time matter? Elapsed time must be accurate? yes yes Need to add or subtract time? Store duration no no Store with zone offset Store with zone ID Store as UTC no yes yes for begin and end Store interval
  • 26. Illustration by John Tenniel, Alice in Wonderland Start at the beginning, and go on til you come to the end: then stop.