13) Dates & Time (java.time)
Goal
Use modern Java date/time classes correctly.
Use java.time (not Date/Calendar for new code)
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(2000, 1, 1);
Period age = Period.between(birthday, today);
Time zones
ZonedDateTime utc = ZonedDateTime.now(ZoneId.of("UTC"));
ZonedDateTime colombo = ZonedDateTime.now(ZoneId.of("Asia/Colombo"));
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(today.format(fmt));
Exercises
- Parse a date string
2026-01-06 into LocalDate.
- Compute days between two dates.
- Print current time in 3 time zones.
Table of contents
- Getting Started: Install, run, and your first program
- Java Basics: types, variables, operators, formatting
- Control Flow: if/switch/loops
- Methods: parameters, return values, overloading
- OOP: classes, objects, encapsulation
- Inheritance & Polymorphism (and when not to use them)
- Interfaces, abstract classes, and design basics
- Exceptions and error handling
- Strings, files, and I/O basics
- Collections: List/Set/Map and Big-O intuition
- Generics (the useful parts)
- Lambdas & Streams
- Dates and time (java.time)
- Testing with JUnit 5 (basics)
- Concurrency: threads, executors, futures
- JVM basics: memory, GC, performance habits
- Build tools: Maven essentials (recommended)
- Next steps: projects to build