Java LocalDate Class: Methods and Examples - Quiz

Total: 5 questions

1. 

How do you create a LocalDate object in Java?

LocalDate has no public constructor — you create it with static factory methods:

  • LocalDate.of(2024, 3, 15) or LocalDate.of(2024, Month.MARCH, 15) — a specific date (months start at 1);
  • LocalDate.now() — today in the JVM default time zone, LocalDate.now(ZoneId.of("Asia/Tokyo")) — today in the given zone;
  • LocalDate.parse("2024-03-15") — parses an ISO-8601 yyyy-MM-dd string;
  • LocalDate.ofYearDay(2001, 32) (2001-02-01) and LocalDate.ofEpochDay(0) (1970-01-01).

An impossible date does not roll over into the next month: LocalDate.of(2023, 2, 29) throws DateTimeException, and LocalDate.parse("2023-1-5") throws DateTimeParseException.

2. 

Why does date stay the same after calling date.plusDays(1);?

LocalDate is immutable. The plus...(), minus...() and with...() methods never modify the object — they return a new date. If you do not assign the result, it is lost:

date.plusDays(1);        // result thrown away
date = date.plusDays(1); // correct

Because it is immutable, LocalDate is thread-safe: one instance can be shared between threads without synchronization and stored in a static final field.

3. 

How do you compare two LocalDate values, and why is == wrong?

Use isBefore(), isAfter() and isEqual(), which return a boolean; equality can also be checked with equals(). The == operator compares references, not values, so for two different objects holding the same date it usually returns false.

For sorting, use compareTo() (LocalDate implements Comparable). It does not return strictly -1/0/1 but a difference: LocalDate.of(2001, 1, 1).compareTo(LocalDate.of(2005, 6, 1)) returns -4. Always compare the result with zero (< 0, > 0), never with -1.

4. 

How do you calculate the number of days between two LocalDate values?

Use ChronoUnit.DAYS.between(start, end) or the equivalent start.until(end, ChronoUnit.DAYS):

LocalDate start = LocalDate.of(2001, 1, 1);
LocalDate end = LocalDate.of(2001, 3, 1);
ChronoUnit.DAYS.between(start, end); // 59
ChronoUnit.DAYS.between(end, start); // -59

The result is a long, the end date is exclusive, and if the end is before the start, the number is negative. Months and years are counted the same way: ChronoUnit.MONTHS.between(start, end) returns 2.

5. 

How do you convert LocalDate to java.util.Date and java.sql.Date and back?

java.util.Date is an instant on the timeline, while LocalDate has neither a time nor a zone, so you have to choose a zone when converting:

ZoneId zone = ZoneId.systemDefault();
Date utilDate = Date.from(localDate.atStartOfDay(zone).toInstant()); // midnight in zone
LocalDate back = utilDate.toInstant().atZone(zone).toLocalDate();

With java.sql.Date no zone is needed: java.sql.Date.valueOf(localDate) and sqlDate.toLocalDate(). Note that sqlDate.toInstant() throws UnsupportedOperationException. Convert there and back with the same ZoneId, otherwise the date can shift by one day.

Page 1 of 1