Java LocalTime Class: Current Time, Formatting and Methods - Quiz

Total: 6 questions

1. 

What is LocalTime in Java and how is it different from LocalDateTime, ZonedDateTime and Instant?

java.time.LocalTime is an immutable, thread-safe class from Java 8 that stores only a time of day (hours, minutes, seconds, nanoseconds) with no date and no time zone, for example 13:45:30.123456789. It fits schedules, opening hours and alarms.

LocalDateTime adds a date to the time, ZonedDateTime adds a time zone as well, and Instant is an exact point on the UTC timeline (timestamps, logs). You turn a LocalTime into a LocalDateTime with atDate(LocalDate) and go back with toLocalTime(); to get an Instant you also need a zone: time.atDate(date).atZone(zone).toInstant().

2. 

How do you get the current time in Java with LocalTime, and how do you make such code testable?

LocalTime.now() returns the time of day from the system clock in the JVM default time zone, including fractions of a second (since Java 9, typically microseconds). For another zone use LocalTime.now(ZoneId.of("Asia/Tokyo")). When you show the time to a user, format it or truncate it: LocalTime.now().truncatedTo(ChronoUnit.SECONDS).

For tests, pass a Clock into the class and call LocalTime.now(clock): in production use Clock.systemDefaultZone(), in a test use Clock.fixed(Instant.parse("2024-03-15T10:00:00Z"), ZoneOffset.UTC), and the result is always 10:00.

3. 

What constants does LocalTime have, and what are the bounds of a time of day?

LocalTime has four constants: MIN and MIDNIGHT are the start of the day, 00:00 (equal values, LocalTime.MIN.equals(LocalTime.MIDNIGHT) returns true), NOON is 12:00, and MAX is the last nanosecond of the day, 23:59:59.999999999.

There is no "24:00": LocalTime.of(24, 0) throws DateTimeException. The same bound shows up in seconds since midnight: toSecondOfDay() returns 0 to 86399 (LocalTime.MAX gives 86399 because fractions of a second are dropped), and LocalTime.ofSecondOfDay(86400) throws DateTimeException. For the upper bound of an "until end of day" range, use LocalTime.MAX.

4. 

How do you compare two LocalTime values and check whether a time falls into a range, including a range across midnight?

For order use isBefore() and isAfter() (strictly earlier / strictly later); for equality use equals(), not ==, which compares references. compareTo() returns a negative number, 0 or a positive number, so LocalTime values can be sorted. For example, LocalTime.of(12, 0).equals(LocalTime.NOON) is true.

A regular range (9:00–18:00): !t.isBefore(from) && t.isBefore(to). When the range crosses midnight (22:00–06:00), the start is later than the end, so t.isAfter(from) && t.isBefore(to) is always false. You need "or": t.isAfter(from) || t.isBefore(to) — for 02:00 this gives true.

5. 

What is the difference between the with...() methods and truncatedTo() in LocalTime?

withHour(), withMinute(), withSecond(), withNano() and with(ChronoField, long) replace one field and keep the others: LocalTime.of(13, 45, 30).withHour(8) gives 08:45:30. An out-of-range value (withHour(25)) throws DateTimeException.

truncatedTo(ChronoUnit) zeroes every field smaller than the given unit: for 13:45:30.123456789, truncatedTo(ChronoUnit.MINUTES) returns 13:45. This is handy for comparing now() values "to the minute". Units longer than a day (MONTHS, YEARS) throw UnsupportedTemporalTypeException. Neither method changes the original object — both return a new one.

6. 

What is the difference between LocalTime.now() and System.nanoTime(), and which one should you use to measure how fast code runs?

LocalTime.now() answers "what time is it?" — the time of day from the system clock. System.nanoTime() answers "how much time has passed?": it returns a long number of nanoseconds from an arbitrary origin; the number itself has nothing to do with the time of day and only makes sense as the difference between two calls.

To measure how fast code runs, use System.nanoTime(): its readings do not depend on adjustments of the system clock (manual changes, NTP sync). The difference between two LocalTime.now() calls can even be negative if the clock was set back or the measurement crossed midnight.

Page 1 of 1