Creating LocalDate Instances: now, of, parse
You'll learn the three main factory methods for creating LocalDate objects to represent calendar dates without time zones.
The Core Idea
Unlike older date classes that used constructors, LocalDate provides static factory methods that make your intent crystal clear. Think of these as three different "date sources": today's date from the system clock, a specific date you define explicitly, or a date parsed from text.
The Three Factory Methods
now() — Getting Today's Date
LocalDate.now() retrieves the current date from your system clock:
LocalDate today = LocalDate.now();
// Example result: 2024-01-15
This is your go-to when you need "today" in your application's logic.
of() — Building Specific Dates
LocalDate.of(int year, int month, int dayOfMonth) constructs a date with explicit values:
LocalDate birthday = LocalDate.of(1990, 5, 23);
// May 23, 1990
LocalDate newYear = LocalDate.of(2025, 1, 1);
// January 1, 2025
You control every component. Month values are intuitive (1 = January, unlike the old Calendar class where 0 = January).
parse() — Converting Strings to Dates
LocalDate.parse(CharSequence text) converts ISO-8601 formatted strings (YYYY-MM-DD):
LocalDate fromString = LocalDate.parse("2023-12-25");
// December 25, 2023
Perfect for reading dates from configuration files, databases, or user input that's already formatted correctly.
Choosing the Right Method
- now() → "What's the date right now?"
- of() → "I know exactly which date I need"
- parse() → "I have a date string and need a LocalDate object"
All three return immutable LocalDate objects that are thread-safe and perfect for representing calendar dates without time-of-day information.
Key Takeaway: Use LocalDate.now() for the current date, of() to construct specific dates with explicit year/month/day values, and parse() to convert ISO-8601 formatted strings into LocalDate objects.