Java Date and Time API: java.time and the Current Date - Quiz
Total: 6 questions
1. What is the Java Date and Time API (java.time) and which classes does it include?
What is the Java Date and Time API (java.time) and which classes does it include?
The Java Date and Time API is the java.time package (JSR-310), the standard library for dates and times added in Java 8 to replace java.util.Date and java.util.Calendar.
Main classes:
LocalDate— a date without a time (2026-10-17);LocalTime— a time of day (14:35:07);LocalDateTime— a date and time without a time zone;ZonedDateTime— a date and time in a specific zone (America/New_York);Instant— a moment on the UTC time-line;PeriodandDuration— amounts of time in years/months/days and in seconds/nanoseconds.
All of them are immutable and thread-safe. The point-in-time classes implement java.time.temporal.Temporal, the amount classes implement java.time.temporal.TemporalAmount.
2. How do you get the current date and time in Java?
How do you get the current date and time in Java?
With the static now() method of the right java.time class:
LocalDate.now()— the date only:2026-10-17;LocalTime.now()— the time only:14:35:07.123456;LocalDateTime.now()— the date and time:2026-10-17T14:35:07.123456;ZonedDateTime.now()— the date and time with the JVM default zone:2026-10-17T14:35:07.123456-04:00[America/New_York];Instant.now()— the current moment, printed in UTC:2026-10-17T18:35:07.123456Z.
The precision of the fraction depends on the Java version: milliseconds in Java 8, usually microseconds since Java 9. To print the date in a fixed format such as MM/dd/yyyy, use LocalDate.now().format(DateTimeFormatter.ofPattern("MM/dd/yyyy")).
3. Why can LocalDateTime.now() on a server show a different time than on your computer, and what are now(ZoneId) and now(Clock) for?
Why can LocalDateTime.now() on a server show a different time than on your computer, and what are now(ZoneId) and now(Clock) for?
The no-argument now() uses the JVM default time zone (ZoneId.systemDefault()), which comes from the OS settings; it is simply now(Clock.systemDefaultZone()). If the server runs in UTC and you are in New York, the time differs by 4 hours, and late in the evening even the date is different.
now(ZoneId)returns the time in an explicitly given zone:LocalDateTime.now(ZoneId.of("America/New_York")).now(Clock)takes the time from aClockobject. Production code passes the system clock, and a unit test passesClock.fixed(Instant.parse("2026-10-18T01:00:00Z"), ZoneId.of("America/New_York")): thenLocalDate.now(clock)always returns2026-10-17, and the test does not depend on the day it runs.
Other options are starting the JVM with -Duser.timezone=America/New_York or storing moments as Instant and converting to a zone only for display.
4. What is the difference between LocalDateTime, ZonedDateTime and Instant? Does Instant store a time zone?
What is the difference between LocalDateTime, ZonedDateTime and Instant? Does Instant store a time zone?
LocalDateTime— a wall-clock date and time without a zone:2026-10-17T14:35:07. Use it for a meeting in one city or the time on a receipt.ZonedDateTime— a date, time and time zone:2026-10-17T14:35:07-04:00[America/New_York]. Use it for flights, calls across countries and daylight saving time.Instant— seconds and nanoseconds since 1970-01-01T00:00:00Z, an exact moment. Use it for timestamps in logs, databases and messages between services.
No, Instant does not store a time zone: it is just a point on the UTC time-line, so toString() always prints it in UTC (the Z suffix). A time zone exists only in ZonedDateTime (a region such as America/New_York) and OffsetDateTime (a fixed offset such as -04:00). To see the local time of an Instant, you must name a zone: instant.atZone(ZoneId.of("America/New_York")).
5. Why is java.time better than java.util.Date and java.util.Calendar?
Why is java.time better than java.util.Date and java.util.Calendar?
- Immutability.
DateandCalendarare changed bysetTime(),set(),add(); injava.timemethods such asplusDays()return a new object. - Thread safety. The old classes and
SimpleDateFormatare not thread-safe; thejava.timeclasses, includingDateTimeFormatter, are. - Numbering. In the old API months start at 0, and
new Date(y, m, d)counts the year from 1900:new Date(2024, 1, 15)is February 15, 3924. Injava.timemonths start at 1 and there is theMonthenum:LocalDate.of(2024, 2, 15). - Validation.
Calendarsilently rolls Feb 29, 2023 over to Mar 1, 2023, whileLocalDate.of(2023, 2, 29)throwsDateTimeException. - Separation of concepts.
Dateis a date, a time and a moment at once;java.timehas one class per task.
6. How do you convert a java.util.Date to a LocalDate or LocalDateTime and back?
How do you convert a java.util.Date to a LocalDate or LocalDateTime and back?
Through Instant, and always with a time zone: a Date is a moment in time (milliseconds since the epoch), while LocalDate and LocalDateTime have no zone.
Date→Instant:date.toInstant(); back:Date.from(instant);Date→LocalDate:date.toInstant().atZone(zone).toLocalDate()orLocalDate.ofInstant(date.toInstant(), zone)in Java 9+;Date→LocalDateTime:LocalDateTime.ofInstant(date.toInstant(), zone); back:Date.from(ldt.atZone(zone).toInstant());GregorianCalendar↔ZonedDateTime:calendar.toZonedDateTime()/GregorianCalendar.from(zdt);java.sql.Date↔LocalDate:sqlDate.toLocalDate()/java.sql.Date.valueOf(localDate).
Example: new Date(0L) is 1970-01-01 in UTC but 1969-12-31T19:00 in America/New_York. Convert only at the boundary with legacy code: JDBC 4.2 and Hibernate 5+ work with LocalDate, LocalDateTime and Instant directly.