3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create and display dates in Java.
- Use LocalDate and LocalTime classes.
- Format dates and times.
- Perform date and time calculations.
- Apply Java date and time functions in applications.
3.2 Overview
Java provides modern date and time APIs used to manage dates, times, schedules, and calculations in software applications. The Java Time API introduced in Java 8 simplifies date handling and improves readability and reliability.
This lesson introduces learners to practical date and time operations in Java.
Date and time handling are important in:
- banking systems,
- booking systems,
- scheduling applications,
- enterprise software,
- and database systems.
Understanding Java date and time functionality is important because most software systems rely on date processing.
PA1301 — Create and Display Dates
Java uses LocalDate to handle dates.
Java Example:
import java.time.LocalDate;
LocalDate today = LocalDate.now();
System.out.println(today);
Practical Activity
Learners must:
- import LocalDate,
- create date objects,
- and display current dates.
PA1302 — Create and Display Time
Java uses LocalTime to manage time values.
Java Example:
import java.time.LocalTime;
LocalTime currentTime = LocalTime.now();
System.out.println(currentTime);
Practical Activity
Learners must:
- create time objects,
- display current time,
- and test Java time methods.
PA1303 — Create Date and Time Together
Java uses LocalDateTime to combine dates and times.
Java Example:
import java.time.LocalDateTime;
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
Practical Activity
Learners must:
- create LocalDateTime objects,
- display date and time,
- and test combined outputs.
PA1304 — Format Dates and Times
Java supports formatting using DateTimeFormatter.
Java Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate today = LocalDate.now();
DateTimeFormatter format =
DateTimeFormatter.ofPattern(“dd/MM/yyyy”);
System.out.println(today.format(format));
Practical Activity
Learners must:
- format dates,
- apply formatting patterns,
- and display formatted output.
PA1305 — Perform Date Calculations
Java allows addition and subtraction of dates.
Java Example:
LocalDate today = LocalDate.now();
LocalDate futureDate = today.plusDays(10);
System.out.println(futureDate);
Practical Activity
Learners must:
- add days,
- subtract dates,
- and display calculated results.
PA1306 — Perform Time Calculations
Java supports time calculations using built-in methods.
Java Example:
LocalTime time = LocalTime.now();
LocalTime later = time.plusHours(2);
System.out.println(later);
Practical Activity
Learners must:
- add hours,
- subtract minutes,
- and display updated times.
PA1307 — Compare Dates
Dates can be compared using comparison methods.
Java Example:
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.of(2025, 1, 1);
System.out.println(date1.isAfter(date2));
Practical Activity
Learners must:
- compare dates,
- determine earlier and later dates,
- and display results.
PA1308 — Create Complete Date and Time Program
Date and time APIs are used in real-world Java applications.
Java Example:
import java.time.LocalDate;
import java.time.LocalTime;
public class DateExample {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalTime currentTime = LocalTime.now();
System.out.println(today);
System.out.println(currentTime);
}
}
Importance of Java Date and Time
Java date and time APIs support:
- scheduling,
- automation,
- calculations,
- and enterprise application development.
3.5 Key Notes / Summary
- Java uses LocalDate for dates.
- Java uses LocalTime for times.
- LocalDateTime combines date and time.
- DateTimeFormatter formats date output.
- Java supports date and time calculations.
- Date comparison methods simplify date processing.