Date and time API
31 questions in 9 subtopics.
Java.time types
Give an overview of the mainjava.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.Why was java.time (JSR 310) added in Java 8? What problems of java.util.Date, Calendar and SimpleDateFormat does it fix?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?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
Explain the difference between Instant,LocalDateTime, OffsetDateTime and ZonedDateTime. Which of them represents a single unambiguous moment?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?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?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
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);
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?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?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.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
What is the difference between Duration and Period? What doDuration.ofDays(1) and Period.ofDays(1) do when added to a ZonedDateTime on a DST change day?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));
Duration.between(Instant.now(), ...) at start and end. Is that correct, and what would you use instead?Formatting and parsing
An old service uses a staticSimpleDateFormat shared across threads and sometimes produces wrong dates or NumberFormatException under load. Explain and fix it.DateTimeFormatter.ofPattern("YYYY-MM-dd") formats 2024-12-30 as 2025-12-30. What is wrong?How does DateTimeFormatter handle locale and resolution? Explain ResolverStyle STRICT, SMART and LENIENT and why parsing 2023-02-30 behaves differently under each.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?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
Rewrite this to be testable:class Coupon {
boolean isExpired() {
return LocalDate.now().isAfter(expiry);
}
}
Legacy Date and Calendar migration
Convert between legacy andjava.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.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.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
Usingjava.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.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
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.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.No question matches these choices.