LocalDate: A Java class that represents a date without time. Created using the of() method.
Note: The method takes year, month, and day parameters in that order.
- Import the java.time.LocalDate package
- Use the LocalDate.of() method
- Pass year, month, and day as parameters
- Store the result in a variable
import java.time.LocalDate;
Pass year (2024), month (3), and day (15) in that order.
The variable now holds the date March 15, 2024.
LocalDate date = LocalDate.of(2024, 3, 15);
LocalDate date = LocalDate.of(2024, 3, 15);
• Method order: Year, Month, Day (Y-M-D)
• Type declaration: Use LocalDate to hold date values
• Static method: of() is a static method of LocalDate
• Practice Tip: Remember Y-M-D order to avoid confusion
- LocalDate birthday = LocalDate.of(2023, 12, 25);
- LocalDate today = LocalDate.of(2024, 1, 1);
- LocalDate halloween = LocalDate.of(2024, 10, 31);
- Always pass parameters in Year-Month-Day order
- Months are numbered 1-12 (not 0-11)
- Day must be valid for the given month
Q: What if I enter an invalid date?
A: Java will throw an exception. For example, LocalDate.of(2024, 2, 30) would fail because February doesn't have 30 days.
Q: Can I create LocalDate with time too?
A: No, LocalDate only stores date. Use LocalDateTime for date and time.
Date comparison: Using comparison methods to determine the relationship between two dates.
Note: LocalDate objects can be compared using methods like isBefore(), isAfter(), and equals().
- Create two LocalDate objects
- Use comparison methods like isBefore() or isAfter()
- Store or use the boolean result
LocalDate date1 = LocalDate.of(2024, 3, 15);
LocalDate date2 = LocalDate.of(2024, 3, 20);
boolean isEarlier = date1.isBefore(date2);
Since March 15 is before March 20, the result is true.
LocalDate date2 = LocalDate.of(2024, 3, 20);
boolean isEarlier = date1.isBefore(date2); // Returns true
Yes, March 15, 2024 is earlier than March 20, 2024.
• Comparison methods: Use isBefore(), isAfter(), equals()
• Boolean result: Comparison returns true or false
• Chronological order: Earlier dates are before later dates
• Practice Tip: Test with known date relationships first
- LocalDate.of(2024, 1, 1).isBefore(LocalDate.of(2024, 12, 31)) // true
- LocalDate.of(2024, 5, 15).isAfter(LocalDate.of(2024, 3, 10)) // true
- LocalDate.of(2024, 4, 1).equals(LocalDate.of(2024, 4, 1)) // true
- isBefore() means "is earlier than"
- isAfter() means "is later than"
- equals() means "is the same date as"
Q: What methods can I use for date comparison?
A: You can use isBefore(), isAfter(), equals(), and compareTo() methods.
Q: Does time matter in date comparison?
A: No! LocalDate only contains date information, not time.
isAfter() method: Returns true if the calling LocalDate is later in time than the argument.
Note: For dates, "greater than" means chronologically later.
- Create two LocalDate objects
- Call isAfter() on the first with the second as argument
- Evaluate the boolean result
LocalDate date1 = LocalDate.of(2024, 6, 10);
LocalDate date2 = LocalDate.of(2024, 3, 15);
boolean isLater = date1.isAfter(date2);
Since June 10 is after March 15, the result is true.
LocalDate date2 = LocalDate.of(2024, 3, 15);
boolean isLater = date1.isAfter(date2); // Returns true
Yes, June 10, 2024 is greater than March 15, 2024.
• isAfter method: Returns true if calling date is later
• Chronological logic: Later dates are greater than earlier dates
• Boolean evaluation: Result is always true or false
• Practice Tip: Think of the number line for date ordering
- LocalDate.of(2024, 12, 31).isAfter(LocalDate.of(2024, 1, 1)) // true
- LocalDate.of(2024, 8, 15).isAfter(LocalDate.of(2024, 8, 10)) // true
- LocalDate.of(2024, 2, 1).isAfter(LocalDate.of(2024, 12, 31)) // false
- isAfter() means "comes later in time"
- Think of dates on a timeline from left to right
- Any date in June isAfter() any date in March of the same year
Q: What if I compare dates in different years?
A: The year is considered first, then month, then day. LocalDate.of(2023, 12, 31).isBefore(LocalDate.of(2024, 1, 1)) returns true.
Q: Can I chain multiple comparisons?
A: Yes! You can use expressions like date1.isAfter(date2) && date2.isAfter(date3).
isBefore() method: Returns true if the calling LocalDate is earlier in time than the argument.
Note: For dates, "less than" means chronologically earlier.
- Create two LocalDate objects
- Call isBefore() on the first with the second as argument
- Evaluate the boolean result
LocalDate date1 = LocalDate.of(2024, 1, 1);
LocalDate date2 = LocalDate.of(2024, 2, 14);
boolean isEarlier = date1.isBefore(date2);
Since January 1 is before February 14, the result is true.
LocalDate date2 = LocalDate.of(2024, 2, 14);
boolean isEarlier = date1.isBefore(date2); // Returns true
Yes, January 1, 2024 is less than February 14, 2024.
• isBefore method: Returns true if calling date is earlier
• Chronological logic: Earlier dates are less than later dates
• Boolean evaluation: Result is always true or false
• Practice Tip: Remember: isBefore() means "earlier than"
- LocalDate.of(2024, 1, 1).isBefore(LocalDate.of(2024, 12, 31)) // true
- LocalDate.of(2024, 5, 1).isBefore(LocalDate.of(2024, 5, 15)) // true
- LocalDate.of(2024, 10, 31).isBefore(LocalDate.of(2024, 9, 15)) // false
- isBefore() means "comes earlier in time"
- Think of the calendar year from January to December
- Any date in January isBefore() any date in February of the same year
Q: What happens if I compare the same dates?
A: LocalDate.of(2024, 5, 1).isBefore(LocalDate.of(2024, 5, 1)) returns false since they're equal.
Q: Can I use isBefore() with other conditions?
A: Yes! You can combine with other methods: date1.isBefore(date2) && date2.isBefore(date3).
equals() method: Returns true if both LocalDate objects represent the same date.
Note: For dates, equality means identical year, month, and day.
- Create two LocalDate objects
- Call equals() on the first with the second as argument
- Evaluate the boolean result
LocalDate date1 = LocalDate.of(2024, 3, 15);
LocalDate date2 = LocalDate.of(2024, 3, 15);
boolean isEqual = date1.equals(date2);
Since both dates are identical, the result is true.
LocalDate date2 = LocalDate.of(2024, 3, 15);
boolean isEqual = date1.equals(date2); // Returns true
Yes, March 15, 2024 and March 15, 2024 are equal.
• equals method: Returns true if dates are identical
• Exact match: Year, month, and day must all match
• Boolean evaluation: Result is always true or false
• Practice Tip: Use equals() to check for specific dates
- LocalDate.of(2024, 12, 25).equals(LocalDate.of(2024, 12, 25)) // true
- LocalDate.of(2024, 1, 1).equals(LocalDate.of(2023, 1, 1)) // false
- LocalDate.of(2024, 7, 4).equals(LocalDate.of(2024, 7, 4)) // true
- equals() checks for exact date match
- Year, month, and day must all be the same
- Use for validating specific dates like birthdays
Q: What if dates have different times?
A: LocalDate doesn't store time, so only dates matter for equality.
Q: Can I check inequality with !equals()?
A: Yes! LocalDate.of(2024, 3, 15).equals(LocalDate.of(2024, 3, 16)) returns false.