SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
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.

Weitere ähnliche Inhalte

Was ist angesagt?

openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollideropenFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperCollider
openFrameworks、サウンド機能・音響合成、ofxMaxim, ofxOsc, ofxPd, ofxSuperColliderAtsushi 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 JavaScriptRaphael Cruzeiro
 
Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Steffen Wenz
 
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 189Mahmoud Samir Fayed
 
Class 24: Imperative Programming
Class 24: Imperative ProgrammingClass 24: Imperative Programming
Class 24: Imperative ProgrammingDavid Evans
 
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 Tornadoemptysquare
 
Weka วิเคราะห์ 55102011029
Weka วิเคราะห์ 55102011029Weka วิเคราะห์ 55102011029
Weka วิเคราะห์ 55102011029so_so37
 
Query History of a Software Project
Query History of a Software ProjectQuery History of a Software Project
Query History of a Software Projectstevensreinout
 
"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
 
Kotlin Coroutines Reloaded
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines ReloadedRoman Elizarov
 
Python opcodes
Python opcodesPython opcodes
Python opcodesalexgolec
 
Cilk Plus Parallel Reduction
Cilk Plus Parallel ReductionCilk Plus Parallel Reduction
Cilk Plus Parallel ReductionAlbert DeFusco
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
Time brings all things to pass
Time brings all things to passTime brings all things to pass
Time brings all things to passKamil Witecki
 
Kotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is comingKirill Rozov
 

Was ist angesagt? (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
 

Andere mochten auch

OSGi and Eclipse RCP
OSGi and Eclipse RCPOSGi and Eclipse RCP
OSGi and Eclipse RCPEric Jain
 
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)Andy Davies
 
CAA Portfolio
CAA PortfolioCAA Portfolio
CAA Portfoliobeast
 
Combining your data with Zenobase
Combining your data with ZenobaseCombining your data with Zenobase
Combining your data with ZenobaseEric Jain
 
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 MatchAriel Ferrante
 
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 UpdateAriel Ferrante
 
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)Andy Davies
 
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...Ariel Ferrante
 
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 MarketplaceAriel Ferrante
 
Faster Frontends
Faster FrontendsFaster Frontends
Faster FrontendsAndy Davies
 
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 Grupo Los Nogales
 

Andere mochten auch (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
 

Ähnlich wie Java Time Puzzlers

rules, events and workflow
rules, events and workflowrules, events and workflow
rules, events and workflowMark Proctor
 
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 8Serhii Kartashov
 
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 .pdfmarketing413921
 
Codestrong 2012 breakout session hacking titanium
Codestrong 2012 breakout session   hacking titaniumCodestrong 2012 breakout session   hacking titanium
Codestrong 2012 breakout session hacking titaniumAxway Appcelerator
 
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()Kiwamu Okabe
 
Deep dumpster diving 2010
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010RonnBlack
 
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 4Kenji HASUNUMA
 
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 toolboxesThinHunh47
 
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.pdfadhityalapcare
 
C vs Java: Finding Prime Numbers
C vs Java: Finding Prime NumbersC vs Java: Finding Prime Numbers
C vs Java: Finding Prime NumbersAdam Feldscher
 
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 4Kenji HASUNUMA
 
Node.js flow control
Node.js flow controlNode.js flow control
Node.js flow controlSimon Su
 
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.pdfanfenterprises
 
PyParis - weather and climate data
PyParis - weather and climate dataPyParis - weather and climate data
PyParis - weather and climate dataMargriet Groenendijk
 
e computer notes - Date time functions
e computer notes - Date time functionse computer notes - Date time functions
e computer notes - Date time functionsecomputernotes
 

Ähnlich wie 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
 

Kürzlich hochgeladen

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 FresherRemote DBA Services
 
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...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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...Drew Madelung
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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 2024The Digital Insurer
 
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 WoodJuan lago vázquez
 
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 TerraformAndrey Devyatkin
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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 DiscoveryTrustArc
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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 educationjfdjdjcjdnsjd
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

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
 
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...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
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
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
+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...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

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.