java-tutorial

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"));

Formatting

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(today.format(fmt));

Exercises

  1. Parse a date string 2026-01-06 into LocalDate.
  2. Compute days between two dates.
  3. Print current time in 3 time zones.

Table of contents

  1. Getting Started: Install, run, and your first program
  2. Java Basics: types, variables, operators, formatting
  3. Control Flow: if/switch/loops
  4. Methods: parameters, return values, overloading
  5. OOP: classes, objects, encapsulation
  6. Inheritance & Polymorphism (and when not to use them)
  7. Interfaces, abstract classes, and design basics
  8. Exceptions and error handling
  9. Strings, files, and I/O basics
  10. Collections: List/Set/Map and Big-O intuition
  11. Generics (the useful parts)
  12. Lambdas & Streams
  13. Dates and time (java.time)
  14. Testing with JUnit 5 (basics)
  15. Concurrency: threads, executors, futures
  16. JVM basics: memory, GC, performance habits
  17. Build tools: Maven essentials (recommended)
  18. Next steps: projects to build