The LocalDate
class in Java, part of the java.time
package introduced in Java 8, is designed for representing date-only values without time or time-zone information. It follows the ISO-8601 calendar system and is ideal for scenarios where only the date is needed, such as birthdays, anniversaries, and historical events. Here are the key features of LocalDate
.
Key Features
1. Immutable and Thread-Safe
- Immutability: Once a
LocalDate
object is created, it cannot be altered. This ensures consistency and predictability in date handling. - Thread-Safety: Because
LocalDate
objects are immutable, they can be shared across multiple threads without synchronization.
2. Easy Instance Creation
- Current Date:
LocalDate.now()
returns the current date. - Specific Date:
LocalDate.of(int year, int month, int dayOfMonth)
creates a specific date. - Parsing Date:
LocalDate.parse(CharSequence text)
converts a string to aLocalDate
.
3. Date Manipulation
- Addition/Subtraction: Methods like
plusDays()
,plusMonths()
,minusDays()
, andminusMonths()
allow for easy date arithmetic. - Field Modification:
withDayOfMonth()
,withMonth()
, andwithYear()
enable creating new instances with modified date parts.
4. Accessing Date Information
- Components Retrieval:
getYear()
,getMonth()
,getDayOfMonth()
, andgetDayOfWeek()
methods allow for retrieving specific date components. - Comparison: Methods such as
isBefore()
,isAfter()
, andisEqual()
facilitate date comparisons.
5. Date Information
- Month and Year Length:
lengthOfMonth()
andlengthOfYear()
provide the number of days in the month or year. - Leap Year Check:
isLeapYear()
determines if the year is a leap year.
6. Formatting and Parsing
- Custom Formatting:
DateTimeFormatter
can be used to formatLocalDate
to a string and parse a string toLocalDate
. - Built-in Formats: Supports ISO-8601 and custom date formats.
7. Integration with Other java.time
Classes
- Combining with Time: Can be paired with
LocalTime
to createLocalDateTime
. - Periods and Durations: Works with
Period
to handle date-based amounts of time.
Java LocalDate Class Methods
The table below contains common methods of the Java LocalDate class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.
Method | Description |
---|---|
atStartOfDay() | Returns a LocalDateTime formed from this date at the start of the day. |
atTime() | Combines this date with a time to create a LocalDateTime . |
equals() | Checks if this date is equal to another date. |
format() | Formats this date using the specified formatter. |
from() | Obtains an instance of LocalDate from a temporal object. |
get() | Gets the value of the specified field from this date as an int . |
getDayOfMonth() | Gets the day-of-month field. |
getDayOfWeek() | Gets the day-of-week field. |
getDayOfYear() | Gets the day-of-year field. |
getMonth() | Gets the month-of-year field using the Month enum. |
getMonthValue() | Gets the month-of-year field from 1 to 12. |
getYear() | Gets the year field. |
isAfter() | Checks if this date is after the specified date. |
isBefore() | Checks if this date is before the specified date. |
isEqual() | Checks if this date is equal to the specified date. |
isLeapYear() | Checks if the year is a leap year, according to the ISO proleptic calendar system rules. |
minusDays() | Returns a copy of this date with the specified number of days subtracted. |
minusMonths() | Returns a copy of this date with the specified number of months subtracted. |
minusWeeks() | Returns a copy of this date with the specified number of weeks subtracted. |
minusYears() | Returns a copy of this date with the specified number of years subtracted. |
now() | Obtains the current date from the system clock in the default time-zone. |
of() | Obtains an instance of LocalDate from a year, month and day. |
parse() | Obtains an instance of LocalDate from a text string such as 2007-12-03 . |
plusDays() | Returns a copy of this date with the specified number of days added. |
plusMonths() | Returns a copy of this date with the specified number of months added. |
plusWeeks() | Returns a copy of this date with the specified number of weeks added. |
withDayOfMonth() | Returns a copy of this date with the day-of-month altered. |
withDayOfYear() | Returns a copy of this date with the day-of-year altered. |
withMonth() | Returns a copy of this date with the month-of-year altered. |
withYear() | Returns a copy of this date with the year altered. |
In conclusion, the java.time.LocalDate
class provides various methods to work with dates, allowing operations such as combining with time, formatting, parsing, and altering date fields. For more detailed information, refer to the official Java Documentation.