Successfully reported this slideshow.
Your SlideShare is downloading. ×

Java Time Puzzlers

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Zone.js 2017
Zone.js 2017
Loading in …3
×

Check these out next

1 of 26 Ad
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

Similar to Java Time Puzzlers (20)

Recently uploaded (20)

Advertisement

Java Time Puzzlers

  1. 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. 2.  2010-08-03T03:30Z a. sunrise b. sunset c. could be either d. that's no sun!
  3. 3.  2010-08-03T03:30Z a. sunrise b. sunset c. could be either d. that's no sun!
  4. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 26. Illustration by John Tenniel, Alice in Wonderland Start at the beginning, and go on til you come to the end: then stop.

×