Java Time Puzzlers

Eric Jain
Eric JainZenobase LLC
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.
1 of 26

More Related Content

What's hot(20)

AaAa
Aa
Akshay Kumar102 views
What they don't tell you about JavaScriptWhat they don't tell you about JavaScript
What they don't tell you about JavaScript
Raphael Cruzeiro359 views
Debugging TV Frame 0x0DDebugging TV Frame 0x0D
Debugging TV Frame 0x0D
Dmitry Vostokov101 views
Class 24: Imperative ProgrammingClass 24: Imperative Programming
Class 24: Imperative Programming
David Evans855 views
5 1. character processing5 1. character processing
5 1. character processing
웅식 전509 views
JavaScript @ CTKJavaScript @ CTK
JavaScript @ CTK
Jakob Mattsson517 views
Query History of a Software ProjectQuery History of a Software Project
Query History of a Software Project
stevensreinout410 views
Kotlin Coroutines ReloadedKotlin Coroutines Reloaded
Kotlin Coroutines Reloaded
Roman Elizarov3K views
Python opcodesPython opcodes
Python opcodes
alexgolec5K views
Cilk Plus Parallel ReductionCilk Plus Parallel Reduction
Cilk Plus Parallel Reduction
Albert DeFusco582 views
Snake.cSnake.c
Snake.c
Vijay Singh144 views
Time brings all things to passTime brings all things to pass
Time brings all things to pass
Kamil Witecki80 views
Kotlin Coroutines. Flow is comingKotlin Coroutines. Flow is coming
Kotlin Coroutines. Flow is coming
Kirill Rozov3.3K views

Similar to Java Time Puzzlers(20)

Clock For MyClock For My
Clock For My
shamalanamnam79 views
Environmental analysis of crop trials - Van EttenEnvironmental analysis of crop trials - Van Etten
Environmental analysis of crop trials - Van Etten
CCAFS | CGIAR Research Program on Climate Change, Agriculture and Food Security373 views
rules, events and workflowrules, events and workflow
rules, events and workflow
Mark Proctor1.8K views
JSR 310. New Date API in Java 8JSR 310. New Date API in Java 8
JSR 310. New Date API in Java 8
Serhii Kartashov1.5K views
GroovyGroovy
Groovy
Zen Urban1.1K views
Deep dumpster diving 2010Deep dumpster diving 2010
Deep dumpster diving 2010
RonnBlack423 views
jkfdlsajfklafjjkfdlsajfklafj
jkfdlsajfklafj
PlanetExpressATX733 views
docdoc
doc
PlanetExpressATX596 views
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
Kenji HASUNUMA65 views
C vs Java: Finding Prime NumbersC vs Java: Finding Prime Numbers
C vs Java: Finding Prime Numbers
Adam Feldscher61 views
Introduction to Date and Time API 4Introduction to Date and Time API 4
Introduction to Date and Time API 4
Kenji HASUNUMA99 views
Node.js flow controlNode.js flow control
Node.js flow control
Simon Su2K views
PyParis - weather and climate dataPyParis - weather and climate data
PyParis - weather and climate data
Margriet Groenendijk276 views
e computer notes - Date time functionse computer notes - Date time functions
e computer notes - Date time functions
ecomputernotes261 views
New Java Date/Time APINew Java Date/Time API
New Java Date/Time API
Juliet Nkwor104 views

Recently uploaded(20)

Liqid: Composable CXL PreviewLiqid: Composable CXL Preview
Liqid: Composable CXL Preview
CXL Forum114 views
ThroughputThroughput
Throughput
Moisés Armani Ramírez25 views
Green Leaf Consulting: Capabilities DeckGreen Leaf Consulting: Capabilities Deck
Green Leaf Consulting: Capabilities Deck
GreenLeafConsulting147 views
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
METHOD AND SYSTEM FOR PREDICTING OPTIMAL LOAD FOR WHICH THE YIELD IS MAXIMUM ...
Prity Khastgir IPR Strategic India Patent Attorney Amplify Innovation22 views
ChatGPT and AI for Web DevelopersChatGPT and AI for Web Developers
ChatGPT and AI for Web Developers
Maximiliano Firtman143 views
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet44 views

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.