Date & Time
LocalDate Introduction
When tracking birthdays, deadlines, or scheduling events, you need to work with calendar dates. LocalDate provides a clean API for date manipulation without the complexity of time zones, replacing the error-prone legacy Date class.
LocalDate represents a date without time in the ISO calendar system (year-month-day). It's part of the java.time package introduced in Java 8.
Getting Current Date
Current.java
Replay: real traced execution (multi-file project)
// Current date
import java.time.LocalDate;
public class Current {
public static void main(String[] args) {
// Static replay uses a fixed sample "today".
LocalDate today = LocalDate.of(2025, 1, 29);
System.out.println("Sample today: " + today);
// Date components
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
System.out.println("\nDate components:");
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Day: " + day);
// Day of week
System.out.println("Day of week: " + today.getDayOfWeek());
// Day of year
System.out.println("Day of year: " + today.getDayOfYear());
// Month name
System.out.println("Month name: " + today.getMonth());
// Is leap year
System.out.println("Leap year: " + today.isLeapYear());
// Length of month
System.out.println("Days in month: " + today.lengthOfMonth());
// Length of year
System.out.println("Days in year: " + today.lengthOfYear());
}
}
today ← 2025-01-29, year ← 2025, month ← 1, day ← 29
7public static void main(String[] args) {8 // Static replay uses a fixed sample "today".9 LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);10 System.out.println("Sample today: " + today2025-01-29);1112 // Date components13 int year→ 2025 = today.getYear();14 int month→ 1 = today.getMonthValue();15 int day→ 29 = today.getDayOfMonth();1617 System.out.println("\nDate components:");18 System.out.println("Year: " + year2025);19 System.out.println("Month: " + month1);20 System.out.println("Day: " + day29);2122 // Day of week23 System.out.println("Day of week: " + today.getDayOfWeek());2425 // Day of year26 System.out.println("Day of year: " + today.getDayOfYear());2728 // Month name29 System.out.println("Month name: " + today.getMonth());3031 // Is leap year32 System.out.println("Leap year: " + today.isLeapYear());3334 // Length of month35 System.out.println("Days in month: " + today.lengthOfMonth());3637 // Length of year38 System.out.println("Days in year: " + today.lengthOfYear());39}outputSample today: 2025-01-29 Date components: Year: 2025 Month: 1 Day: 29 Day of week: WEDNESDAY Day of year: 29 Month name: JANUARY Leap year: false Days in month: 31 Days in year: 365
LocalDate
An immutable date object representing year, month, and day without time or timezone information.
Creating Specific Dates
Create.java
Replay: real traced execution (multi-file project)
// Create specific date
import java.time.LocalDate;
import java.time.Month;
public class Create {
public static void main(String[] args) {
// Create with year, month, day
LocalDate date1 = LocalDate.of(2025, 1, 29);
System.out.println("Date 1: " + date1);
// Create with Month enum
LocalDate date2 = LocalDate.of(2025, Month.JANUARY, 29);
System.out.println("Date 2: " + date2);
// Special dates
LocalDate newYear = LocalDate.of(2025, 1, 1);
System.out.println("\nNew Year 2025: " + newYear);
LocalDate christmas = LocalDate.of(2025, 12, 25);
System.out.println("Christmas 2025: " + christmas);
// First day of month
LocalDate today = LocalDate.of(2025, 1, 29);
LocalDate firstOfMonth = today.withDayOfMonth(1);
System.out.println("\nFirst of this month: " + firstOfMonth);
// Last day of month
LocalDate lastOfMonth = today.withDayOfMonth(today.lengthOfMonth());
System.out.println("Last of this month: " + lastOfMonth);
// First day of year
LocalDate firstOfYear = today.withDayOfYear(1);
System.out.println("First of this year: " + firstOfYear);
// Change components
LocalDate modified = date1.withYear(2026).withMonth(6).withDayOfMonth(15);
System.out.println("\nModified date: " + modified);
// Min and max dates
LocalDate min = LocalDate.MIN;
LocalDate max = LocalDate.MAX;
System.out.println("\nMin date: " + min);
System.out.println("Max date: " + max);
// Epoch (1970-01-01)
LocalDate epoch = LocalDate.ofEpochDay(0);
System.out.println("Epoch: " + epoch);
}
}
// Create specific date
import java.time.LocalDate;
import java.time.Month;
public class Create {
public static void main(String[] args) {
// Create with year, month, day
LocalDate date1 = LocalDate.of(2024, 2, 29);
System.out.println("Date 1: " + date1);
// Create with Month enum
LocalDate date2 = LocalDate.of(2025, Month.JANUARY, 29);
System.out.println("Date 2: " + date2);
// Special dates
LocalDate newYear = LocalDate.of(2025, 1, 1);
System.out.println("\nNew Year 2025: " + newYear);
LocalDate christmas = LocalDate.of(2025, 12, 25);
System.out.println("Christmas 2025: " + christmas);
// First day of month
LocalDate today = LocalDate.of(2025, 1, 29);
LocalDate firstOfMonth = today.withDayOfMonth(1);
System.out.println("\nFirst of this month: " + firstOfMonth);
// Last day of month
LocalDate lastOfMonth = today.withDayOfMonth(today.lengthOfMonth());
System.out.println("Last of this month: " + lastOfMonth);
// First day of year
LocalDate firstOfYear = today.withDayOfYear(1);
System.out.println("First of this year: " + firstOfYear);
// Change components
LocalDate modified = date1.withYear(2026).withMonth(6).withDayOfMonth(15);
System.out.println("\nModified date: " + modified);
// Min and max dates
LocalDate min = LocalDate.MIN;
LocalDate max = LocalDate.MAX;
System.out.println("\nMin date: " + min);
System.out.println("Max date: " + max);
// Epoch (1970-01-01)
LocalDate epoch = LocalDate.ofEpochDay(0);
System.out.println("Epoch: " + epoch);
}
}
date1 ← 2025-01-29, date2 ← 2025-01-29, newYear ← 2025-01-01, christmas ← 2025-12-25
8public static void main(String[] args) {9 // Create with year, month, day10 LocalDate date1→ 2025-01-29 = LocalDate.of(2025, 1, 29); //@date1=LocalDate.of(2025, 1, 29), LocalDate.of(2024, 2, 29)11 System.out.println("Date 1: " + date12025-01-29);1213 // Create with Month enum14 LocalDate date2→ 2025-01-29 = LocalDate.of(2025, Month.JANUARY, 29);15 System.out.println("Date 2: " + date22025-01-29);1617 // Special dates18 LocalDate newYear→ 2025-01-01 = LocalDate.of(2025, 1, 1);19 System.out.println("\nNew Year 2025: " + newYear2025-01-01);2021 LocalDate christmas→ 2025-12-25 = LocalDate.of(2025, 12, 25);22 System.out.println("Christmas 2025: " + christmas2025-12-25);2324 // First day of month25 LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);26 LocalDate firstOfMonth→ 2025-01-01 = today.withDayOfMonth(1);27 System.out.println("\nFirst of this month: " + firstOfMonth2025-01-01);2829 // Last day of month30 LocalDate lastOfMonth→ 2025-01-31 = today.withDayOfMonth(today.lengthOfMonth());31 System.out.println("Last of this month: " + lastOfMonth2025-01-31);3233 // First day of year34 LocalDate firstOfYear→ 2025-01-01 = today.withDayOfYear(1);35 System.out.println("First of this year: " + firstOfYear2025-01-01);3637 // Change components38 LocalDate modified→ 2026-06-15 = date1.withYear(2026).withMonth(6).withDayOfMonth(15);39 System.out.println("\nModified date: " + modified2026-06-15);4041 // Min and max dates42 LocalDate min→ -999999999-01-01 = LocalDate.MIN;43 LocalDate max→ +999999999-12-31 = LocalDate.MAX;44 System.out.println("\nMin date: " + min-999999999-01-01);45 System.out.println("Max date: " + max+999999999-12-31);4647 // Epoch (1970-01-01)48 LocalDate epoch→ 1970-01-01 = LocalDate.ofEpochDay(0);49 System.out.println("Epoch: " + epoch1970-01-01);50}outputDate 1: 2025-01-29 Date 2: 2025-01-29 New Year 2025: 2025-01-01 Christmas 2025: 2025-12-25 First of this month: 2025-01-01 Last of this month: 2025-01-31 First of this year: 2025-01-01 Modified date: 2026-06-15 Min date: -999999999-01-01 Max date: +999999999-12-31 Epoch: 1970-01-01
date1 ← 2024-02-29, date2 ← 2025-01-29, newYear ← 2025-01-01, christmas ← 2025-12-25
8public static void main(String[] args) {9 // Create with year, month, day10 LocalDate date1→ 2024-02-29 = LocalDate.of(2024, 2, 29);11 System.out.println("Date 1: " + date12024-02-29);1213 // Create with Month enum14 LocalDate date2→ 2025-01-29 = LocalDate.of(2025, Month.JANUARY, 29);15 System.out.println("Date 2: " + date22025-01-29);1617 // Special dates18 LocalDate newYear→ 2025-01-01 = LocalDate.of(2025, 1, 1);19 System.out.println("\nNew Year 2025: " + newYear2025-01-01);2021 LocalDate christmas→ 2025-12-25 = LocalDate.of(2025, 12, 25);22 System.out.println("Christmas 2025: " + christmas2025-12-25);2324 // First day of month25 LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);26 LocalDate firstOfMonth→ 2025-01-01 = today.withDayOfMonth(1);27 System.out.println("\nFirst of this month: " + firstOfMonth2025-01-01);2829 // Last day of month30 LocalDate lastOfMonth→ 2025-01-31 = today.withDayOfMonth(today.lengthOfMonth());31 System.out.println("Last of this month: " + lastOfMonth2025-01-31);3233 // First day of year34 LocalDate firstOfYear→ 2025-01-01 = today.withDayOfYear(1);35 System.out.println("First of this year: " + firstOfYear2025-01-01);3637 // Change components38 LocalDate modified→ 2026-06-15 = date1.withYear(2026).withMonth(6).withDayOfMonth(15);39 System.out.println("\nModified date: " + modified2026-06-15);4041 // Min and max dates42 LocalDate min→ -999999999-01-01 = LocalDate.MIN;43 LocalDate max→ +999999999-12-31 = LocalDate.MAX;44 System.out.println("\nMin date: " + min-999999999-01-01);45 System.out.println("Max date: " + max+999999999-12-31);4647 // Epoch (1970-01-01)48 LocalDate epoch→ 1970-01-01 = LocalDate.ofEpochDay(0);49 System.out.println("Epoch: " + epoch1970-01-01);50}outputDate 1: 2024-02-29 Date 2: 2025-01-29 New Year 2025: 2025-01-01 Christmas 2025: 2025-12-25 First of this month: 2025-01-01 Last of this month: 2025-01-31 First of this year: 2025-01-01 Modified date: 2026-06-15 Min date: -999999999-01-01 Max date: +999999999-12-31 Epoch: 1970-01-01
Date Components
Components.java
Replay: real traced execution (multi-file project)
// Date components
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.Month;
public class Components {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2025, 1, 29);
System.out.println("Date: " + date);
System.out.println();
// Year, month, day
System.out.println("Year: " + date.getYear());
System.out.println("Month (int): " + date.getMonthValue());
System.out.println("Day: " + date.getDayOfMonth());
// Month enum
Month month = date.getMonth();
System.out.println("\nMonth enum: " + month);
System.out.println("Month name: " + month.name());
System.out.println("Month value: " + month.getValue());
// Day of week
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println("\nDay of week enum: " + dayOfWeek);
System.out.println("Day name: " + dayOfWeek.name());
System.out.println("Day value: " + dayOfWeek.getValue()); // 1=Monday, 7=Sunday
// Day of year
System.out.println("\nDay of year: " + date.getDayOfYear());
// Era
System.out.println("Era: " + date.getEra()); // CE or BCE
// Check properties
System.out.println("\nIs leap year: " + date.isLeapYear());
System.out.println("Length of month: " + date.lengthOfMonth());
System.out.println("Length of year: " + date.lengthOfYear());
// Convert to epoch day
long epochDay = date.toEpochDay();
System.out.println("\nDays since epoch (1970-01-01): " + epochDay);
// Multiple dates
System.out.println("\nWeek dates:");
LocalDate monday = LocalDate.of(2025, 1, 27);
for (int i = 0; i < 7; i++) {
LocalDate day = monday.plusDays(i);
System.out.println(day + " is " + day.getDayOfWeek());
}
}
}
// Date components
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.Month;
public class Components {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2024, 2, 29);
System.out.println("Date: " + date);
System.out.println();
// Year, month, day
System.out.println("Year: " + date.getYear());
System.out.println("Month (int): " + date.getMonthValue());
System.out.println("Day: " + date.getDayOfMonth());
// Month enum
Month month = date.getMonth();
System.out.println("\nMonth enum: " + month);
System.out.println("Month name: " + month.name());
System.out.println("Month value: " + month.getValue());
// Day of week
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println("\nDay of week enum: " + dayOfWeek);
System.out.println("Day name: " + dayOfWeek.name());
System.out.println("Day value: " + dayOfWeek.getValue()); // 1=Monday, 7=Sunday
// Day of year
System.out.println("\nDay of year: " + date.getDayOfYear());
// Era
System.out.println("Era: " + date.getEra()); // CE or BCE
// Check properties
System.out.println("\nIs leap year: " + date.isLeapYear());
System.out.println("Length of month: " + date.lengthOfMonth());
System.out.println("Length of year: " + date.lengthOfYear());
// Convert to epoch day
long epochDay = date.toEpochDay();
System.out.println("\nDays since epoch (1970-01-01): " + epochDay);
// Multiple dates
System.out.println("\nWeek dates:");
LocalDate monday = LocalDate.of(2025, 1, 27);
for (int i = 0; i < 7; i++) {
LocalDate day = monday.plusDays(i);
System.out.println(day + " is " + day.getDayOfWeek());
}
}
}
date ← 2025-01-29, month ← JANUARY, dayOfWeek ← WEDNESDAY, epochDay ← 20117
9public static void main(String[] args) {10 LocalDate date→ 2025-01-29 = LocalDate.of(2025, 1, 29); //@date=LocalDate.of(2025, 1, 29), LocalDate.of(2024, 2, 29)11 12 System.out.println("Date: " + date2025-01-29);13 System.out.println();1415 // Year, month, day16 System.out.println("Year: " + date.getYear());17 System.out.println("Month (int): " + date.getMonthValue());18 System.out.println("Day: " + date.getDayOfMonth());1920 // Month enum21 Month month→ JANUARY = date.getMonth();22 System.out.println("\nMonth enum: " + monthJANUARY);23 System.out.println("Month name: " + month.name());24 System.out.println("Month value: " + month.getValue());2526 // Day of week27 DayOfWeek dayOfWeek→ WEDNESDAY = date.getDayOfWeek();28 System.out.println("\nDay of week enum: " + dayOfWeekWEDNESDAY);29 System.out.println("Day name: " + dayOfWeek.name());30 System.out.println("Day value: " + dayOfWeek.getValue()); // 1=Monday, 7=Sunday3132 // Day of year33 System.out.println("\nDay of year: " + date.getDayOfYear());3435 // Era36 System.out.println("Era: " + date.getEra()); // CE or BCE3738 // Check properties39 System.out.println("\nIs leap year: " + date.isLeapYear());40 System.out.println("Length of month: " + date.lengthOfMonth());41 System.out.println("Length of year: " + date.lengthOfYear());4243 // Convert to epoch day44 long epochDay→ 20117 = date.toEpochDay();45 System.out.println("\nDays since epoch (1970-01-01): " + epochDay20117);4647 // Multiple dates48 System.out.println("\nWeek dates:");49 LocalDate monday→ 2025-01-27 = LocalDate.of(2025, 1, 27);50 for (int i = 0; i < 7; i++) {outputDate: 2025-01-29 Year: 2025 Month (int): 1 Day: 29 Month enum: JANUARY Month name: JANUARY Month value: 1 Day of week enum: WEDNESDAY Day name: WEDNESDAY Day value: 3 Day of year: 29 Era: CE Is leap year: false Length of month: 31 Length of year: 365 Days since epoch (1970-01-01): 20117 Week dates:day ← 2025-01-27
pass 1 of 749LocalDate monday = LocalDate.of(2025, 1, 27);50for (int i0 = 0; i < 7; i++) {51 LocalDate day→ 2025-01-27 = monday.plusDays(i0);52 System.out.println(day2025-01-27 + " is " + day.getDayOfWeek());53}output2025-01-27 is MONDAYAll 7 passes — pass 1 is the card above pass iday1 0 2025-01-27 2 1 2025-01-28 3 2 2025-01-29 4 3 2025-01-30 5 4 2025-01-31 6 5 2025-02-01 7 6 2025-02-02
date ← 2024-02-29, month ← FEBRUARY, dayOfWeek ← THURSDAY, epochDay ← 19782
9public static void main(String[] args) {10 LocalDate date→ 2024-02-29 = LocalDate.of(2024, 2, 29);11 12 System.out.println("Date: " + date2024-02-29);13 System.out.println();1415 // Year, month, day16 System.out.println("Year: " + date.getYear());17 System.out.println("Month (int): " + date.getMonthValue());18 System.out.println("Day: " + date.getDayOfMonth());1920 // Month enum21 Month month→ FEBRUARY = date.getMonth();22 System.out.println("\nMonth enum: " + monthFEBRUARY);23 System.out.println("Month name: " + month.name());24 System.out.println("Month value: " + month.getValue());2526 // Day of week27 DayOfWeek dayOfWeek→ THURSDAY = date.getDayOfWeek();28 System.out.println("\nDay of week enum: " + dayOfWeekTHURSDAY);29 System.out.println("Day name: " + dayOfWeek.name());30 System.out.println("Day value: " + dayOfWeek.getValue()); // 1=Monday, 7=Sunday3132 // Day of year33 System.out.println("\nDay of year: " + date.getDayOfYear());3435 // Era36 System.out.println("Era: " + date.getEra()); // CE or BCE3738 // Check properties39 System.out.println("\nIs leap year: " + date.isLeapYear());40 System.out.println("Length of month: " + date.lengthOfMonth());41 System.out.println("Length of year: " + date.lengthOfYear());4243 // Convert to epoch day44 long epochDay→ 19782 = date.toEpochDay();45 System.out.println("\nDays since epoch (1970-01-01): " + epochDay19782);4647 // Multiple dates48 System.out.println("\nWeek dates:");49 LocalDate monday→ 2025-01-27 = LocalDate.of(2025, 1, 27);50 for (int i = 0; i < 7; i++) {outputDate: 2024-02-29 Year: 2024 Month (int): 2 Day: 29 Month enum: FEBRUARY Month name: FEBRUARY Month value: 2 Day of week enum: THURSDAY Day name: THURSDAY Day value: 4 Day of year: 60 Era: CE Is leap year: true Length of month: 29 Length of year: 366 Days since epoch (1970-01-01): 19782 Week dates:day ← 2025-01-27
pass 1 of 749LocalDate monday = LocalDate.of(2025, 1, 27);50for (int i0 = 0; i < 7; i++) {51 LocalDate day→ 2025-01-27 = monday.plusDays(i0);52 System.out.println(day2025-01-27 + " is " + day.getDayOfWeek());53}output2025-01-27 is MONDAYAll 7 passes — pass 1 is the card above pass iday1 0 2025-01-27 2 1 2025-01-28 3 2 2025-01-29 4 3 2025-01-30 5 4 2025-01-31 6 5 2025-02-01 7 6 2025-02-02
date_components
Individual parts of a date that can be extracted: year, month (1-12), day of month, day of week, day of year.
Comparing Dates
Compare.java
Replay: real traced execution (multi-file project)
// Compare dates
import java.time.LocalDate;
public class Compare {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2025, 1, 29);
LocalDate date2 = LocalDate.of(2025, 2, 15);
LocalDate date3 = LocalDate.of(2025, 1, 29);
System.out.println("Date 1: " + date1);
System.out.println("Date 2: " + date2);
System.out.println("Date 3: " + date3);
System.out.println();
// isBefore, isAfter
System.out.println("date1.isBefore(date2): " + date1.isBefore(date2));
System.out.println("date1.isAfter(date2): " + date1.isAfter(date2));
// equals
System.out.println("\ndate1.equals(date2): " + date1.equals(date2));
System.out.println("date1.equals(date3): " + date1.equals(date3));
// compareTo
System.out.println("\ndate1.compareTo(date2): " + date1.compareTo(date2)); // negative
System.out.println("date2.compareTo(date1): " + date2.compareTo(date1)); // positive
System.out.println("date1.compareTo(date3): " + date1.compareTo(date3)); // zero
// isEqual (handles null better)
System.out.println("\ndate1.isEqual(date3): " + date1.isEqual(date3));
// Today comparisons
LocalDate today = LocalDate.of(2025, 1, 29);
System.out.println("\nSample today: " + today);
System.out.println("date1 is before today: " + date1.isBefore(today));
System.out.println("date1 is after today: " + date1.isAfter(today));
// Find earliest/latest
LocalDate earliest = date1.isBefore(date2) ? date1 : date2;
LocalDate latest = date1.isAfter(date2) ? date1 : date2;
System.out.println("\nEarliest: " + earliest);
System.out.println("Latest: " + latest);
// Check if in range
LocalDate check = LocalDate.of(2025, 1, 31);
LocalDate start = LocalDate.of(2025, 1, 1);
LocalDate end = LocalDate.of(2025, 2, 1);
boolean inRange = !check.isBefore(start) && !check.isAfter(end);
System.out.println("\n" + check + " is in range [" + start + ", " + end + "]: " + inRange);
// Sort dates
LocalDate[] dates = {
LocalDate.of(2025, 3, 15),
LocalDate.of(2025, 1, 10),
LocalDate.of(2025, 2, 20)
};
System.out.println("\nBefore sort:");
for (LocalDate d : dates) {
System.out.println(" " + d);
}
java.util.Arrays.sort(dates);
System.out.println("After sort:");
for (LocalDate d : dates) {
System.out.println(" " + d);
}
}
}
// Compare dates
import java.time.LocalDate;
public class Compare {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2025, 1, 29);
LocalDate date2 = LocalDate.of(2025, 2, 15);
LocalDate date3 = LocalDate.of(2025, 1, 29);
System.out.println("Date 1: " + date1);
System.out.println("Date 2: " + date2);
System.out.println("Date 3: " + date3);
System.out.println();
// isBefore, isAfter
System.out.println("date1.isBefore(date2): " + date1.isBefore(date2));
System.out.println("date1.isAfter(date2): " + date1.isAfter(date2));
// equals
System.out.println("\ndate1.equals(date2): " + date1.equals(date2));
System.out.println("date1.equals(date3): " + date1.equals(date3));
// compareTo
System.out.println("\ndate1.compareTo(date2): " + date1.compareTo(date2)); // negative
System.out.println("date2.compareTo(date1): " + date2.compareTo(date1)); // positive
System.out.println("date1.compareTo(date3): " + date1.compareTo(date3)); // zero
// isEqual (handles null better)
System.out.println("\ndate1.isEqual(date3): " + date1.isEqual(date3));
// Today comparisons
LocalDate today = LocalDate.of(2025, 1, 29);
System.out.println("\nSample today: " + today);
System.out.println("date1 is before today: " + date1.isBefore(today));
System.out.println("date1 is after today: " + date1.isAfter(today));
// Find earliest/latest
LocalDate earliest = date1.isBefore(date2) ? date1 : date2;
LocalDate latest = date1.isAfter(date2) ? date1 : date2;
System.out.println("\nEarliest: " + earliest);
System.out.println("Latest: " + latest);
// Check if in range
LocalDate check = LocalDate.of(2025, 2, 15);
LocalDate start = LocalDate.of(2025, 1, 1);
LocalDate end = LocalDate.of(2025, 2, 1);
boolean inRange = !check.isBefore(start) && !check.isAfter(end);
System.out.println("\n" + check + " is in range [" + start + ", " + end + "]: " + inRange);
// Sort dates
LocalDate[] dates = {
LocalDate.of(2025, 3, 15),
LocalDate.of(2025, 1, 10),
LocalDate.of(2025, 2, 20)
};
System.out.println("\nBefore sort:");
for (LocalDate d : dates) {
System.out.println(" " + d);
}
java.util.Arrays.sort(dates);
System.out.println("After sort:");
for (LocalDate d : dates) {
System.out.println(" " + d);
}
}
}
date1 ← 2025-01-29, date2 ← 2025-02-15, date3 ← 2025-01-29, today ← 2025-01-29
7public static void main(String[] args) {8 LocalDate date1→ 2025-01-29 = LocalDate.of(2025, 1, 29);9 LocalDate date2→ 2025-02-15 = LocalDate.of(2025, 2, 15);10 LocalDate date3→ 2025-01-29 = LocalDate.of(2025, 1, 29);1112 System.out.println("Date 1: " + date12025-01-29);13 System.out.println("Date 2: " + date22025-02-15);14 System.out.println("Date 3: " + date32025-01-29);15 System.out.println();1617 // isBefore, isAfter18 System.out.println("date1.isBefore(date2): " + date1.isBefore(date22025-02-15));19 System.out.println("date1.isAfter(date2): " + date1.isAfter(date22025-02-15));2021 // equals22 System.out.println("\ndate1.equals(date2): " + date1.equals(date22025-02-15));23 System.out.println("date1.equals(date3): " + date1.equals(date32025-01-29));2425 // compareTo26 System.out.println("\ndate1.compareTo(date2): " + date1.compareTo(date22025-02-15)); // negative27 System.out.println("date2.compareTo(date1): " + date2.compareTo(date12025-01-29)); // positive28 System.out.println("date1.compareTo(date3): " + date1.compareTo(date32025-01-29)); // zero2930 // isEqual (handles null better)31 System.out.println("\ndate1.isEqual(date3): " + date1.isEqual(date32025-01-29));3233 // Today comparisons34 LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);35 System.out.println("\nSample today: " + today2025-01-29);36 System.out.println("date1 is before today: " + date1.isBefore(today2025-01-29));37 System.out.println("date1 is after today: " + date1.isAfter(today2025-01-29));3839 // Find earliest/latest40 LocalDate earliest→ 2025-01-29 = date1.isBefore(date22025-02-15) ? date12025-01-29 : date2;41 LocalDate latest→ 2025-02-15 = date1.isAfter(date22025-02-15) ? date12025-01-29 : date2;42 43 System.out.println("\nEarliest: " + earliest2025-01-29);44 System.out.println("Latest: " + latest2025-02-15);4546 // Check if in range47 LocalDate check→ 2025-01-31 = LocalDate.of(2025, 1, 31); //@check=LocalDate.of(2025, 1, 31), LocalDate.of(2025, 2, 15)48 LocalDate start→ 2025-01-01 = LocalDate.of(2025, 1, 1);49 LocalDate end→ 2025-02-01 = LocalDate.of(2025, 2, 1);5051 boolean inRange→ true = !check.isBefore(start2025-01-01) && !check.isAfter(end2025-02-01);52 System.out.println("\n" + check2025-01-31 + " is in range [" + start2025-01-01 + ", " + end2025-02-01 + "]: " + inRangetrue);5354 // Sort dates55 LocalDate[] dates = {56 LocalDate.of(2025, 3, 15),57 LocalDate.of(2025, 1, 10),58 LocalDate.of(2025, 2, 20)59 };6061 System.out.println("\nBefore sort:");62 for (LocalDate d : dates) {outputDate 1: 2025-01-29 Date 2: 2025-02-15 Date 3: 2025-01-29 date1.isBefore(date2): true date1.isAfter(date2): false date1.equals(date2): false date1.equals(date3): true date1.compareTo(date2): -1 date2.compareTo(date1): 1 date1.compareTo(date3): 0 date1.isEqual(date3): true Sample today: 2025-01-29 date1 is before today: false date1 is after today: false Earliest: 2025-01-29 Latest: 2025-02-15 2025-01-31 is in range [2025-01-01, 2025-02-01]: true Before sort:for (LocalDate d : dates)
pass 1 of 361System.out.println("\nBefore sort:");62for (LocalDate d2025-03-15 : dates) {63 System.out.println(" " + d2025-03-15);64}output 2025-03-15All 3 passes — pass 1 is the card above pass d1 2025-03-15 2 2025-01-10 3 2025-02-20 java.util.Arrays.sort(dates);
66java.util.Arrays.sort(dates);6768System.out.println("After sort:");69for (LocalDate d : dates) {outputAfter sort:for (LocalDate d : dates)
pass 1 of 368System.out.println("After sort:");69for (LocalDate d2025-01-10 : dates) {70 System.out.println(" " + d2025-01-10);71}output 2025-01-10All 3 passes — pass 1 is the card above pass d1 2025-01-10 2 2025-02-20 3 2025-03-15
date1 ← 2025-01-29, date2 ← 2025-02-15, date3 ← 2025-01-29, today ← 2025-01-29
7public static void main(String[] args) {8 LocalDate date1→ 2025-01-29 = LocalDate.of(2025, 1, 29);9 LocalDate date2→ 2025-02-15 = LocalDate.of(2025, 2, 15);10 LocalDate date3→ 2025-01-29 = LocalDate.of(2025, 1, 29);1112 System.out.println("Date 1: " + date12025-01-29);13 System.out.println("Date 2: " + date22025-02-15);14 System.out.println("Date 3: " + date32025-01-29);15 System.out.println();1617 // isBefore, isAfter18 System.out.println("date1.isBefore(date2): " + date1.isBefore(date22025-02-15));19 System.out.println("date1.isAfter(date2): " + date1.isAfter(date22025-02-15));2021 // equals22 System.out.println("\ndate1.equals(date2): " + date1.equals(date22025-02-15));23 System.out.println("date1.equals(date3): " + date1.equals(date32025-01-29));2425 // compareTo26 System.out.println("\ndate1.compareTo(date2): " + date1.compareTo(date22025-02-15)); // negative27 System.out.println("date2.compareTo(date1): " + date2.compareTo(date12025-01-29)); // positive28 System.out.println("date1.compareTo(date3): " + date1.compareTo(date32025-01-29)); // zero2930 // isEqual (handles null better)31 System.out.println("\ndate1.isEqual(date3): " + date1.isEqual(date32025-01-29));3233 // Today comparisons34 LocalDate today→ 2025-01-29 = LocalDate.of(2025, 1, 29);35 System.out.println("\nSample today: " + today2025-01-29);36 System.out.println("date1 is before today: " + date1.isBefore(today2025-01-29));37 System.out.println("date1 is after today: " + date1.isAfter(today2025-01-29));3839 // Find earliest/latest40 LocalDate earliest→ 2025-01-29 = date1.isBefore(date22025-02-15) ? date12025-01-29 : date2;41 LocalDate latest→ 2025-02-15 = date1.isAfter(date22025-02-15) ? date12025-01-29 : date2;42 43 System.out.println("\nEarliest: " + earliest2025-01-29);44 System.out.println("Latest: " + latest2025-02-15);4546 // Check if in range47 LocalDate check→ 2025-02-15 = LocalDate.of(2025, 2, 15);48 LocalDate start→ 2025-01-01 = LocalDate.of(2025, 1, 1);49 LocalDate end→ 2025-02-01 = LocalDate.of(2025, 2, 1);5051 boolean inRange→ false = !check.isBefore(start2025-01-01) && !check.isAfter(end2025-02-01);52 System.out.println("\n" + check2025-02-15 + " is in range [" + start2025-01-01 + ", " + end2025-02-01 + "]: " + inRangefalse);5354 // Sort dates55 LocalDate[] dates = {56 LocalDate.of(2025, 3, 15),57 LocalDate.of(2025, 1, 10),58 LocalDate.of(2025, 2, 20)59 };6061 System.out.println("\nBefore sort:");62 for (LocalDate d : dates) {outputDate 1: 2025-01-29 Date 2: 2025-02-15 Date 3: 2025-01-29 date1.isBefore(date2): true date1.isAfter(date2): false date1.equals(date2): false date1.equals(date3): true date1.compareTo(date2): -1 date2.compareTo(date1): 1 date1.compareTo(date3): 0 date1.isEqual(date3): true Sample today: 2025-01-29 date1 is before today: false date1 is after today: false Earliest: 2025-01-29 Latest: 2025-02-15 2025-02-15 is in range [2025-01-01, 2025-02-01]: false Before sort:for (LocalDate d : dates)
pass 1 of 361System.out.println("\nBefore sort:");62for (LocalDate d2025-03-15 : dates) {63 System.out.println(" " + d2025-03-15);64}output 2025-03-15All 3 passes — pass 1 is the card above pass d1 2025-03-15 2 2025-01-10 3 2025-02-20 java.util.Arrays.sort(dates);
66java.util.Arrays.sort(dates);6768System.out.println("After sort:");69for (LocalDate d : dates) {outputAfter sort:for (LocalDate d : dates)
pass 1 of 368System.out.println("After sort:");69for (LocalDate d2025-01-10 : dates) {70 System.out.println(" " + d2025-01-10);71}output 2025-01-10All 3 passes — pass 1 is the card above pass d1 2025-01-10 2 2025-02-20 3 2025-03-15
immutability
LocalDate cannot be modified after creation. Methods like plusDays() return a new LocalDate instance.
Parsing Dates
Parse.java
Replay: real traced execution (multi-file project)
// Parse date string
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
public class Parse {
public static void main(String[] args) {
// Parse ISO format (yyyy-MM-dd)
String iso = "2025-01-29";
LocalDate date1 = LocalDate.parse(iso);
System.out.println("Parsed ISO: " + date1);
// Parse with custom pattern
String custom1 = "29/01/2025";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date2 = LocalDate.parse(custom1, formatter1);
System.out.println("Parsed " + custom1 + ": " + date2);
// Parse various formats
String[] dateStrings = {
"2025-12-25",
"25/12/2025",
"12-25-2025",
"Dec 25, 2025"
};
DateTimeFormatter[] formatters = {
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ofPattern("dd/MM/yyyy"),
DateTimeFormatter.ofPattern("MM-dd-yyyy"),
DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US)
};
System.out.println("\nParse various formats:");
for (int i = 0; i < dateStrings.length; i++) {
LocalDate date = LocalDate.parse(dateStrings[i], formatters[i]);
System.out.println(dateStrings[i] + " -> " + date);
}
// Handle parse errors
System.out.println("\nHandle parse errors:");
String[] testDates = {
"2025-01-29",
"2025-13-01", // invalid month
"2025-02-30", // invalid day
"not-a-date"
};
for (String dateStr : testDates) {
try {
LocalDate date = LocalDate.parse(dateStr);
System.out.println(dateStr + " -> " + date);
} catch (DateTimeParseException e) {
System.out.println(dateStr + " -> ERROR: " + e.getMessage());
}
}
// Parse with validation
System.out.println("\nSafe parse:");
System.out.println(safeParseDate("2025-01-29"));
System.out.println(safeParseDate("invalid"));
// Parse from different separators
String date3 = "2025/01/29";
LocalDate parsed3 = LocalDate.parse(date3, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println("\nParsed " + date3 + ": " + parsed3);
// Parse with text month
String date4 = "January 29, 2025";
LocalDate parsed4 = LocalDate.parse(date4, DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.US));
System.out.println("Parsed " + date4 + ": " + parsed4);
}
public static LocalDate safeParseDate(String dateStr) {
try {
return LocalDate.parse(dateStr);
} catch (DateTimeParseException e) {
System.out.println("Invalid date: " + dateStr);
return null;
}
}
}
// Parse date string
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Locale;
public class Parse {
public static void main(String[] args) {
// Parse ISO format (yyyy-MM-dd)
String iso = "2024-02-29";
LocalDate date1 = LocalDate.parse(iso);
System.out.println("Parsed ISO: " + date1);
// Parse with custom pattern
String custom1 = "29/01/2025";
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date2 = LocalDate.parse(custom1, formatter1);
System.out.println("Parsed " + custom1 + ": " + date2);
// Parse various formats
String[] dateStrings = {
"2025-12-25",
"25/12/2025",
"12-25-2025",
"Dec 25, 2025"
};
DateTimeFormatter[] formatters = {
DateTimeFormatter.ISO_LOCAL_DATE,
DateTimeFormatter.ofPattern("dd/MM/yyyy"),
DateTimeFormatter.ofPattern("MM-dd-yyyy"),
DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US)
};
System.out.println("\nParse various formats:");
for (int i = 0; i < dateStrings.length; i++) {
LocalDate date = LocalDate.parse(dateStrings[i], formatters[i]);
System.out.println(dateStrings[i] + " -> " + date);
}
// Handle parse errors
System.out.println("\nHandle parse errors:");
String[] testDates = {
"2025-01-29",
"2025-13-01", // invalid month
"2025-02-30", // invalid day
"not-a-date"
};
for (String dateStr : testDates) {
try {
LocalDate date = LocalDate.parse(dateStr);
System.out.println(dateStr + " -> " + date);
} catch (DateTimeParseException e) {
System.out.println(dateStr + " -> ERROR: " + e.getMessage());
}
}
// Parse with validation
System.out.println("\nSafe parse:");
System.out.println(safeParseDate("2025-01-29"));
System.out.println(safeParseDate("invalid"));
// Parse from different separators
String date3 = "2025/01/29";
LocalDate parsed3 = LocalDate.parse(date3, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println("\nParsed " + date3 + ": " + parsed3);
// Parse with text month
String date4 = "January 29, 2025";
LocalDate parsed4 = LocalDate.parse(date4, DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.US));
System.out.println("Parsed " + date4 + ": " + parsed4);
}
public static LocalDate safeParseDate(String dateStr) {
try {
return LocalDate.parse(dateStr);
} catch (DateTimeParseException e) {
System.out.println("Invalid date: " + dateStr);
return null;
}
}
}
iso ← 2025-01-29, date1 ← 2025-01-29, custom1 ← 29/01/2025, formatter1 ← Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD)
10public static void main(String[] args) {11 // Parse ISO format (yyyy-MM-dd)12 String iso→ 2025-01-29 = "2025-01-29"; //@iso="2025-01-29", "2024-02-29"13 LocalDate date1→ 2025-01-29 = LocalDate.parse(iso2025-01-29);14 System.out.println("Parsed ISO: " + date12025-01-29);1516 // Parse with custom pattern17 String custom1→ 29/01/2025 = "29/01/2025";18 DateTimeFormatter formatter1→ Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD) = DateTimeFormatter.ofPattern("dd/MM/yyyy");19 LocalDate date2→ 2025-01-29 = LocalDate.parse(custom129/01/2025, formatter1Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD));20 System.out.println("Parsed " + custom129/01/2025 + ": " + date22025-01-29);2122 // Parse various formats23 String[] dateStrings = {24 "2025-12-25",25 "25/12/2025",26 "12-25-2025",27 "Dec 25, 2025"28 };2930 DateTimeFormatter[] formatters = {31 DateTimeFormatter.ISO_LOCAL_DATE,32 DateTimeFormatter.ofPattern("dd/MM/yyyy"),33 DateTimeFormatter.ofPattern("MM-dd-yyyy"),34 DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US)35 };3637 System.out.println("\nParse various formats:");38 for (int i = 0; i < dateStrings.length; i++) {outputParsed ISO: 2025-01-29 Parsed 29/01/2025: 2025-01-29 Parse various formats:date ← 2025-12-25
pass 1 of 437System.out.println("\nParse various formats:");38for (int i0 = 0; i < dateStrings.length4; i++) {39 LocalDate date→ 2025-12-25 = LocalDate.parse(dateStrings[i]2025-12-25, formatters[i]Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2));40 System.out.println(dateStrings[i]2025-12-25 + " -> " + date2025-12-25);41}output2025-12-25 -> 2025-12-25All 4 passes — pass 1 is the card above pass idateStrings[i]formatters[i]date1 0 2025-12-25 Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2) 2025-12-25 2 1 25/12/2025 Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 3 2 12-25-2025 Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'-'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 4 3 Dec 25, 2025 Text(MonthOfYear,SHORT)' 'Value(DayOfMonth,2)','' 'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 String[] testDates =
43// Handle parse errors44System.out.println("\nHandle parse errors:");45String[] testDates = {46 "2025-01-29",47 "2025-13-01", // invalid month48 "2025-02-30", // invalid day49 "not-a-date"50};output Handle parse errors:for (String dateStr : testDates)
pass 1 of 452for (String dateStr2025-01-29 : testDates) {53 try {All 4 passes — pass 1 is the card above pass dateStr1 2025-01-29 2 2025-13-01 3 2025-02-30 4 not-a-date date ← 2025-01-29
pass 1 of 452for (String dateStr : testDates) {53 try {54 LocalDate date→ 2025-01-29 = LocalDate.parse(dateStr2025-01-29);55 System.out.println(dateStr2025-01-29 + " -> " + date2025-01-29);56 } catch (DateTimeParseException e) {output2025-01-29 -> 2025-01-29All 4 passes — pass 1 is the card above pass dateStrdate1 2025-01-29 2025-01-29 2 2025-13-01 — 3 2025-02-30 — 4 not-a-date — catch (DateTimeParseException e)
pass 1 of 355 System.out.println(dateStr + " -> " + date);56} catch (DateTimeParseException ejava.time.format.DateTimeParseException: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13) {57 System.out.println(dateStr2025-13-01 + " -> ERROR: " + e.getMessage());58}output2025-13-01 -> ERROR: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13All 3 passes — pass 1 is the card above pass edateStr1 java.time.format.DateTimeParseException: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13 2025-13-01 2 java.time.format.DateTimeParseException: Text '2025-02-30' could not be parsed: Invalid date 'FEBRUARY 30' 2025-02-30 3 java.time.format.DateTimeParseException: Text 'not-a-date' could not be parsed at index 0 not-a-date System.out.println(" Safe parse:");
61// Parse with validation62System.out.println("\nSafe parse:");63System.out.println(safeParseDate("2025-01-29"));64System.out.println(safeParseDate("invalid"));output Safe parse:public static LocalDate safeParseDate(String dateStr)
pass 1 of 277public static LocalDate safeParseDate(String dateStr2025-01-29) {78 try {try
pass 1 of 277public static LocalDate safeParseDate(String dateStr) {78 try {79 return LocalDate.parse(dateStr2025-01-29);80 } catch (DateTimeParseException e) {System.out.println(safeParseDate("2025-01-29"));
62System.out.println("\nSafe parse:");63System.out.println(safeParseDate("2025-01-29"));64System.out.println(safeParseDate("invalid"));output2025-01-29public static LocalDate safeParseDate(String dateStr)
pass 2 of 277public static LocalDate safeParseDate(String dateStrinvalid) {78 try {try
pass 2 of 277public static LocalDate safeParseDate(String dateStr) {78 try {79 return LocalDate.parse(dateStrinvalid);80 } catch (DateTimeParseException e) {catch (DateTimeParseException e)
79 return LocalDate.parse(dateStr);80} catch (DateTimeParseException ejava.time.format.DateTimeParseException: Text 'invalid' could not be parsed at index 0) {81 System.out.println("Invalid date: " + dateStrinvalid);82 return null;83}outputInvalid date: invaliddate3 ← 2025/01/29, parsed3 ← 2025-01-29, date4 ← January 29, 2025
63 System.out.println(safeParseDate("2025-01-29"));64 System.out.println(safeParseDate("invalid"));6566 // Parse from different separators67 String date3→ 2025/01/29 = "2025/01/29";68 LocalDate parsed3→ 2025-01-29 = LocalDate.parse(date32025/01/29, DateTimeFormatter.ofPattern("yyyy/MM/dd"));69 System.out.println("\nParsed " + date32025/01/29 + ": " + parsed32025-01-29);7071 // Parse with text month72 String date4→ January 29, 2025 = "January 29, 2025";73 LocalDate parsed4→ 2025-01-29 = LocalDate.parse(date4January 29, 2025, DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.US));74 System.out.println("Parsed " + date4January 29, 2025 + ": " + parsed42025-01-29);75}outputnull Parsed 2025/01/29: 2025-01-29 Parsed January 29, 2025: 2025-01-29
iso ← 2024-02-29, date1 ← 2024-02-29, custom1 ← 29/01/2025, formatter1 ← Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD)
10public static void main(String[] args) {11 // Parse ISO format (yyyy-MM-dd)12 String iso→ 2024-02-29 = "2024-02-29";13 LocalDate date1→ 2024-02-29 = LocalDate.parse(iso2024-02-29);14 System.out.println("Parsed ISO: " + date12024-02-29);1516 // Parse with custom pattern17 String custom1→ 29/01/2025 = "29/01/2025";18 DateTimeFormatter formatter1→ Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD) = DateTimeFormatter.ofPattern("dd/MM/yyyy");19 LocalDate date2→ 2025-01-29 = LocalDate.parse(custom129/01/2025, formatter1Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD));20 System.out.println("Parsed " + custom129/01/2025 + ": " + date22025-01-29);2122 // Parse various formats23 String[] dateStrings = {24 "2025-12-25",25 "25/12/2025",26 "12-25-2025",27 "Dec 25, 2025"28 };2930 DateTimeFormatter[] formatters = {31 DateTimeFormatter.ISO_LOCAL_DATE,32 DateTimeFormatter.ofPattern("dd/MM/yyyy"),33 DateTimeFormatter.ofPattern("MM-dd-yyyy"),34 DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US)35 };3637 System.out.println("\nParse various formats:");38 for (int i = 0; i < dateStrings.length; i++) {outputParsed ISO: 2024-02-29 Parsed 29/01/2025: 2025-01-29 Parse various formats:date ← 2025-12-25
pass 1 of 437System.out.println("\nParse various formats:");38for (int i0 = 0; i < dateStrings.length4; i++) {39 LocalDate date→ 2025-12-25 = LocalDate.parse(dateStrings[i]2025-12-25, formatters[i]Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2));40 System.out.println(dateStrings[i]2025-12-25 + " -> " + date2025-12-25);41}output2025-12-25 -> 2025-12-25All 4 passes — pass 1 is the card above pass idateStrings[i]formatters[i]date1 0 2025-12-25 Value(Year,4,10,EXCEEDS_PAD)'-'Value(MonthOfYear,2)'-'Value(DayOfMonth,2) 2025-12-25 2 1 25/12/2025 Value(DayOfMonth,2)'/'Value(MonthOfYear,2)'/'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 3 2 12-25-2025 Value(MonthOfYear,2)'-'Value(DayOfMonth,2)'-'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 4 3 Dec 25, 2025 Text(MonthOfYear,SHORT)' 'Value(DayOfMonth,2)','' 'Value(YearOfEra,4,19,EXCEEDS_PAD) 2025-12-25 String[] testDates =
43// Handle parse errors44System.out.println("\nHandle parse errors:");45String[] testDates = {46 "2025-01-29",47 "2025-13-01", // invalid month48 "2025-02-30", // invalid day49 "not-a-date"50};output Handle parse errors:for (String dateStr : testDates)
pass 1 of 452for (String dateStr2025-01-29 : testDates) {53 try {All 4 passes — pass 1 is the card above pass dateStr1 2025-01-29 2 2025-13-01 3 2025-02-30 4 not-a-date date ← 2025-01-29
pass 1 of 452for (String dateStr : testDates) {53 try {54 LocalDate date→ 2025-01-29 = LocalDate.parse(dateStr2025-01-29);55 System.out.println(dateStr2025-01-29 + " -> " + date2025-01-29);56 } catch (DateTimeParseException e) {output2025-01-29 -> 2025-01-29All 4 passes — pass 1 is the card above pass dateStrdate1 2025-01-29 2025-01-29 2 2025-13-01 — 3 2025-02-30 — 4 not-a-date — catch (DateTimeParseException e)
pass 1 of 355 System.out.println(dateStr + " -> " + date);56} catch (DateTimeParseException ejava.time.format.DateTimeParseException: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13) {57 System.out.println(dateStr2025-13-01 + " -> ERROR: " + e.getMessage());58}output2025-13-01 -> ERROR: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13All 3 passes — pass 1 is the card above pass edateStr1 java.time.format.DateTimeParseException: Text '2025-13-01' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13 2025-13-01 2 java.time.format.DateTimeParseException: Text '2025-02-30' could not be parsed: Invalid date 'FEBRUARY 30' 2025-02-30 3 java.time.format.DateTimeParseException: Text 'not-a-date' could not be parsed at index 0 not-a-date System.out.println(" Safe parse:");
61// Parse with validation62System.out.println("\nSafe parse:");63System.out.println(safeParseDate("2025-01-29"));64System.out.println(safeParseDate("invalid"));output Safe parse:public static LocalDate safeParseDate(String dateStr)
pass 1 of 277public static LocalDate safeParseDate(String dateStr2025-01-29) {78 try {try
pass 1 of 277public static LocalDate safeParseDate(String dateStr) {78 try {79 return LocalDate.parse(dateStr2025-01-29);80 } catch (DateTimeParseException e) {System.out.println(safeParseDate("2025-01-29"));
62System.out.println("\nSafe parse:");63System.out.println(safeParseDate("2025-01-29"));64System.out.println(safeParseDate("invalid"));output2025-01-29public static LocalDate safeParseDate(String dateStr)
pass 2 of 277public static LocalDate safeParseDate(String dateStrinvalid) {78 try {try
pass 2 of 277public static LocalDate safeParseDate(String dateStr) {78 try {79 return LocalDate.parse(dateStrinvalid);80 } catch (DateTimeParseException e) {catch (DateTimeParseException e)
79 return LocalDate.parse(dateStr);80} catch (DateTimeParseException ejava.time.format.DateTimeParseException: Text 'invalid' could not be parsed at index 0) {81 System.out.println("Invalid date: " + dateStrinvalid);82 return null;83}outputInvalid date: invaliddate3 ← 2025/01/29, parsed3 ← 2025-01-29, date4 ← January 29, 2025
63 System.out.println(safeParseDate("2025-01-29"));64 System.out.println(safeParseDate("invalid"));6566 // Parse from different separators67 String date3→ 2025/01/29 = "2025/01/29";68 LocalDate parsed3→ 2025-01-29 = LocalDate.parse(date32025/01/29, DateTimeFormatter.ofPattern("yyyy/MM/dd"));69 System.out.println("\nParsed " + date32025/01/29 + ": " + parsed32025-01-29);7071 // Parse with text month72 String date4→ January 29, 2025 = "January 29, 2025";73 LocalDate parsed4→ 2025-01-29 = LocalDate.parse(date4January 29, 2025, DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.US));74 System.out.println("Parsed " + date4January 29, 2025 + ": " + parsed42025-01-29);75}outputnull Parsed 2025/01/29: 2025-01-29 Parsed January 29, 2025: 2025-01-29
Key Methods
now(): Current dateof(year, month, day): Create specific dategetYear(),getMonthValue(),getDayOfMonth(): Get componentsplusDays(),minusDays(): Add/subtract daysisBefore(),isAfter(): Compare datesparse(): From string
Common Operations
- Add/subtract days, weeks, months, years
- Compare dates
- Get day of week
- Calculate difference
- Format to string
Exercise: Practical.java
Calculate how many days until the next birthday given a birth date