CodeBlanks Logo Icon CodeBlanks

LocalDate

Learn dates with LocalDate.

Dates with LocalDate

LocalDate represents a date without time or zone: year, month and day. It lives in the java.time package and is immutable.

Example: LocalDate today = LocalDate.now(); or LocalDate bd = LocalDate.of(2000, 5, 15);. To add days to a date you use plusDays(7); this returns a new LocalDate and leaves the old one unchanged. This design keeps date math safe.

Adding and subtracting values

Because LocalDate is immutable, additions and subtractions return a new object. plusDays, plusMonths, plusYears add; minusDays, minusMonths subtract.

Example: LocalDate next = bd.plusDays(7).plusMonths(1);. Here 7 days are added first, then 1 month. Chaining lets you express consecutive operations on one line. The result is always a new LocalDate; the original stays unchanged.

Practice Add seven days to a date

Create a date with LocalDate.of(2024, 1, 15). Add seven days with plusDays(7) and print the result with System.out.println.

Starter code

Main.java
import java.time.LocalDate;

// Create Jan 15 2024 with LocalDate.of
// Add seven days with plusDays
// Print the result

Sample solution

Main.java
import java.time.LocalDate;

public class DatePlusDays {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2024, 1, 15);
        System.out.println(date.plusDays(7));
    }
}

Expected output

2024-01-22

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which class represents a date without time?

  • LocalDateTime
  • LocalTime
  • LocalDate Correct answer
  • Instant

Explanation: LocalDate holds year, month and day; no time.

2 Complete the LocalDate.of call

Question
LocalDate bd = LocalDate.____(2000, 5, 15);
Answer
LocalDate bd = LocalDate.of(2000, 5, 15);

Explanation: of creates a specific date.

3 Match the LocalDate methods

Term Meaning
LocalDate.now Today
LocalDate.of Specific date
plusDays Add days
minusMonths Subtract months

All lessons in Advanced