← Java from Scratch Lesson 48 of 88 · 10 XP

Date and time

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

The java.time package (Java 8+) has immutable date classes:

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

LocalDate d = LocalDate.of(2026, 11, 5);
LocalDate p = LocalDate.parse("2026-12-31");        // the ISO format: YYYY-MM-DD
d.plusDays(30)                    // a new date; d doesn't change
d.getDayOfWeek()                  // THURSDAY
d.isLeapYear()
ChronoUnit.DAYS.between(d, p)     // 56

DateTimeFormatter f = DateTimeFormatter.ofPattern("dd.MM.yyyy");
d.format(f)                       // "05.11.2026"
LocalDate.parse("05.11.2026", f)

LocalTime.of(9, 30), LocalDateTime.now(), Duration.ofMinutes(90)

LocalDate.now() is today's date; we don't use it in tests: the answer would change every day.

It's better not to touch the old Date and Calendar — they're mutable and full of traps (months there start from zero).

Task. The input: two dates in the format dd.mm.yyyy. Print: the number of days between them, the day of the week of the first date (as in DayOfWeek, for example MONDAY) and the first date + 100 days in the same format.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.