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
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.
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.