Scrivio Interview library

Date and time API

31 questions in 9 subtopics.

Java.time types

ConceptFoundations6 min read
Give an overview of the main java.time types and say which one you would use for: a birthday, a store's opening time, a log timestamp, a meeting in New York next month, and an API field with an offset.
ConceptFoundations7 min read
Why was java.time (JSR 310) added in Java 8? What problems of java.util.Date, Calendar and SimpleDateFormat does it fix?
DesignIntermediate7 min read
You are designing the date and time handling for a booking service: guests book hotel nights, check-in is 15:00 local time, and payments record when they happened. Which java.time types do you store and expose, and why?
LeadershipPrincipal7 min read
Time handling bugs keep causing incidents in your company: zone mix-ups, DST jobs and leap-day billing. What organisation-wide measures would you put in place, beyond telling teams to use java.time?

Instant, LocalDateTime, ZonedDateTime, OffsetDateTime

ConceptFoundations6 min read
Explain the difference between Instant, LocalDateTime, OffsetDateTime and ZonedDateTime. Which of them represents a single unambiguous moment?
DebuggingFoundations7 min read
A service stores LocalDateTime.now() in a database column for order times. After deploying to a second region, reports show orders appearing hours in the future. What went wrong?
ScenarioIntermediate7 min read
With JPA and PostgreSQL, a team maps an Instant field to a timestamp column (without time zone) and sees values shifted when the database session time zone changes. How do you make storage correct?
LeadershipAdvanced7 min read
Across many services, timestamps travel as epoch millis, ISO strings with offsets, local strings with no zone and database-specific types. What standard do you set for timestamps in APIs, events and storage?

Time zones and DST

CodingFoundations6 min read
What does this print, assuming America/New_York and the US DST change on 2025-03-09 at 02:00?
ZonedDateTime z = ZonedDateTime.of(2025, 3, 9, 2, 30, 0, 0, ZoneId.of("America/New_York"));
System.out.println(z);
DebuggingFoundations6 min read
A job scheduled with plusHours(24) on a ZonedDateTime runs at 01:00 instead of 02:00 local time on the day after DST ends. Why, and what should the code use?
ScenarioIntermediate6 min read
A government changes its DST rules with a few weeks notice. Your service stores future appointments. What can go wrong, and how do you keep the JVM's time zone data current?
DesignAdvanced6 min read
Design how a global scheduling platform stores and fires recurring events such as 09:00 every Monday in the user's zone, used by many teams.
LeadershipPrincipal6 min read
Time zone rules change several times a year somewhere in the world, and your products operate in dozens of countries. Who in your organisation should own time zone data and policy, and what processes do you put in place for the long term?

Duration and Period

ConceptFoundations6 min read
What is the difference between Duration and Period? What do Duration.ofDays(1) and Period.ofDays(1) do when added to a ZonedDateTime on a DST change day?
CodingFoundations5 min read
What is printed?
LocalDate d = LocalDate.of(2024, 1, 31);
System.out.println(d.plusMonths(1));
System.out.println(d.plusMonths(1).plusMonths(1));
System.out.println(d.plusMonths(2));
CodingIntermediate5 min read
Measure elapsed time for a request handler. A colleague uses Duration.between(Instant.now(), ...) at start and end. Is that correct, and what would you use instead?

Formatting and parsing

DebuggingFoundations7 min read
An old service uses a static SimpleDateFormat shared across threads and sometimes produces wrong dates or NumberFormatException under load. Explain and fix it.
DebuggingFoundations6 min read
DateTimeFormatter.ofPattern("YYYY-MM-dd") formats 2024-12-30 as 2025-12-30. What is wrong?
ConceptIntermediate6 min read
How does DateTimeFormatter handle locale and resolution? Explain ResolverStyle STRICT, SMART and LENIENT and why parsing 2023-02-30 behaves differently under each.
ScenarioIntermediate6 min read
An upgrade from Java 17 to 21 changes how some formatted times look, and a parser now fails on input like "3:30 PM" from a partner. What could have changed and how would you make formatting stable?
LeadershipPrincipal6 min read
Dates have been exchanged between your systems as epoch seconds, epoch millis, ISO-8601 strings, locale-formatted strings and database-native types. What single data-format standard would you set for the next decade, and how do you migrate without breaking consumers?

Clock for testing

CodingFoundations6 min read
Rewrite this to be testable:
class Coupon {
boolean isExpired() {
return LocalDate.now().isAfter(expiry);
}
}
Show how a test would set the date.
DesignIntermediate7 min read
Your team wants to test behaviour that depends on time passing, such as token expiry after 15 minutes and daily rollups. Design a time abstraction for the codebase and the rules for using it.
DesignAdvanced7 min read
Your organisation wants every service to support time-shifted testing, for example running end-of-month billing in a staging environment on any day. How do you design the time source standard so it works across many services and teams?

Legacy Date and Calendar migration

CodingFoundations6 min read
Convert between legacy and java.time types: a java.util.Date to LocalDate in Europe/Berlin, a java.sql.Timestamp to Instant, and a LocalDate back to java.sql.Date.
ScenarioAdvanced7 min read
A large codebase still uses java.util.Date and Calendar in domain models, JDBC code and public library APIs. Plan the migration to java.time across many teams.
LeadershipPrincipal7 min read
java.util.Date shipped in 1996 and its replacement only arrived in 2014. What lessons does that history give for how you choose and retire foundational APIs in your organisation?

Date arithmetic and temporal adjusters

CodingFoundations7 min read
Using java.time, compute: the last day of the current month, the next Monday after a given date (not the same day), the number of days between two LocalDate values, and the date 30 business days after today ignoring holidays.
DesignIntermediate8 min read
Billing must find the due date as the 10th business day of the next month in the customer's country, skipping weekends and public holidays that differ per country. Design this with java.time and explain how you test it.

Measuring elapsed time

ConceptFoundations7 min read
Which Java clock should you use to measure how long a method takes: System.currentTimeMillis(), System.nanoTime() or Instant.now()? Show correct code and explain why the others can give wrong results.
DebuggingIntermediate7 min read
A dashboard of request durations, computed as Instant.now() at the end minus a start Instant stored in the request, shows occasional negative values and some requests taking exactly one hour on one day. Explain the causes and how you would measure correctly across services.