The LocalDate
class in Java 8 represents a date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03
. This guide covers the common methods provided by the LocalDate
class, demonstrating how to create, manipulate, and query date objects.
1. now()
Obtains the current date from the system clock in the default time-zone.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
}
}
Output:
Current Date: 2024-07-05
2. of(int year, int month, int dayOfMonth)
Obtains an instance of LocalDate
from a year, month, and day.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.of(2023, 7, 5);
System.out.println("Specific Date: " + date);
}
}
Output:
Specific Date: 2023-07-05
3. parse(CharSequence text)
Obtains an instance of LocalDate
from a text string such as 2007-12-03
.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.parse("2023-07-05");
System.out.println("Parsed Date: " + date);
}
}
Output:
Parsed Date: 2023-07-05
4. plusDays(long daysToAdd)
Returns a copy of this LocalDate
with the specified number of days added.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.plusDays(10);
System.out.println("Date plus 10 days: " + newDate);
}
}
Output:
Date plus 10 days: 2024-07-15
5. plusWeeks(long weeksToAdd)
Returns a copy of this LocalDate
with the specified number of weeks added.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.plusWeeks(2);
System.out.println("Date plus 2 weeks: " + newDate);
}
}
Output:
Date plus 2 weeks: 2024-07-19
6. plusMonths(long monthsToAdd)
Returns a copy of this LocalDate
with the specified number of months added.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.plusMonths(3);
System.out.println("Date plus 3 months: " + newDate);
}
}
Output:
Date plus 3 months: 2024-10-05
7. plusYears(long yearsToAdd)
Returns a copy of this LocalDate
with the specified number of years added.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.plusYears(1);
System.out.println("Date plus 1 year: " + newDate);
}
}
Output:
Date plus 1 year: 2025-07-05
8. minusDays(long daysToSubtract)
Returns a copy of this LocalDate
with the specified number of days subtracted.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.minusDays(10);
System.out.println("Date minus 10 days: " + newDate);
}
}
Output:
Date minus 10 days: 2024-06-25
9. minusWeeks(long weeksToSubtract)
Returns a copy of this LocalDate
with the specified number of weeks subtracted.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.minusWeeks(2);
System.out.println("Date minus 2 weeks: " + newDate);
}
}
Output:
Date minus 2 weeks: 2024-06-21
10. minusMonths(long monthsToSubtract)
Returns a copy of this LocalDate
with the specified number of months subtracted.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.minusMonths(3);
System.out.println("Date minus 3 months: " + newDate);
}
}
Output:
Date minus 3 months: 2024-04-05
11. minusYears(long yearsToSubtract)
Returns a copy of this LocalDate
with the specified number of years subtracted.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.minusYears(1);
System.out.println("Date minus 1 year: " + newDate);
}
}
Output:
Date minus 1 year: 2023-07-05
12. isAfter(LocalDate other)
Checks if this date is after the specified date.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.now();
LocalDate date2 = date1.minusDays(1);
boolean isAfter = date1.isAfter(date2);
System.out.println("Is after: " + isAfter);
}
}
Output:
Is after: true
13. isBefore(LocalDate other)
Checks if this date is before the specified date.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.now();
LocalDate date2 = date1.plusDays(1);
boolean isBefore = date1.isBefore(date2);
System.out.println("Is before: " + isBefore);
}
}
Output:
Is before: true
14. isEqual(LocalDate other)
Checks if this date is equal to the specified date.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.now();
LocalDate date2 = LocalDate.from(date1);
boolean isEqual = date1.isEqual(date2);
System.out.println("Is equal: " + isEqual);
}
}
Output:
Is equal: true
15. toString()
Outputs this date as a String
, such as 2007-12-03
.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println("Date: " + date.toString());
}
}
Output:
Date: 2024-07-05
16. getYear()
Gets the year field.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int year = date.getYear();
System.out.println("Year: " + year);
}
}
Output:
Year: 2024
17. getMonth()
Gets the month-of-year field using the Month
enum.
Example:
import java.time.LocalDate;
import java.time.Month;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
Month month = date.getMonth();
System.out.println("Month: " + month);
}
}
Output:
Month: JULY
18. getMonthValue()
Gets the month-of-year field from 1 to
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int monthValue = date.getMonthValue();
System.out.println("Month value: " + monthValue);
}
}
Output:
Month value: 7
19. getDayOfMonth()
Gets the day-of-month field.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int dayOfMonth = date.getDayOfMonth();
System.out.println("Day of month: " + dayOfMonth);
}
}
Output:
Day of month: 5
20. getDayOfWeek()
Gets the day-of-week field, which is an enum DayOfWeek
.
Example:
import java.time.LocalDate;
import java.time.DayOfWeek;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
DayOfWeek dayOfWeek = date.getDayOfWeek();
System.out.println("Day of week: " + dayOfWeek);
}
}
Output:
Day of week: FRIDAY
21. getDayOfYear()
Gets the day-of-year field.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int dayOfYear = date.getDayOfYear();
System.out.println("Day of year: " + dayOfYear);
}
}
Output:
Day of year: 187
22. lengthOfMonth()
Returns the length of the month in days.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int lengthOfMonth = date.lengthOfMonth();
System.out.println("Length of month: " + lengthOfMonth);
}
}
Output:
Length of month: 31
23. lengthOfYear()
Returns the length of the year in days, either 365
or 366
.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
int lengthOfYear = date.lengthOfYear();
System.out.println("Length of year: " + lengthOfYear);
}
}
Output:
Length of year: 366
24. isLeapYear()
Checks if the year is a leap year.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
boolean isLeapYear = date.isLeapYear();
System.out.println("Is leap year: " + isLeapYear);
}
}
Output:
Is leap year: true
25. atStartOfDay()
Combines this date with the time of midnight to create a LocalDateTime
at the start of this date.
Example:
import java.time.LocalDate;
import java.time.LocalDateTime;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDateTime startOfDay = date.atStartOfDay();
System.out.println("Start of day: " + startOfDay);
}
}
Output:
Start of day: 2024-07-05T00:00
26. toEpochDay()
Converts this date to the Epoch Day count (the number of days since 1970-01-01
).
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
long epochDay = date.toEpochDay();
System.out.println("Epoch day: " + epochDay);
}
}
Output:
Epoch day: 19909
27. withYear(int year)
Returns a copy of this date with the year altered.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.withYear(2025);
System.out.println("New date with altered year: " + newDate);
}
}
Output:
New date with altered year: 2025-07-05
28. withMonth(int month)
Returns a copy of this date with the month-of-year altered.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.withMonth(12);
System.out.println("New date with altered month: " + newDate);
}
}
Output:
New date with altered month: 2024-12-05
29. withDayOfMonth(int dayOfMonth)
Returns a copy of this date with the day-of-month altered.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.withDayOfMonth(15);
System.out.println("New date with altered day: " + newDate);
}
}
Output:
New date with altered day: 2024-07-15
30. withDayOfYear(int dayOfYear)
Returns a copy of this date with the day-of-year altered.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate newDate = date.withDayOfYear(200);
System.out.println("New date with altered day of year: " + newDate);
}
}
Output:
New date with altered day of year: 2024-07-18
31. isBefore(LocalDate otherDate)
Checks if this date is before the specified date.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2023, 7, 5);
LocalDate date2 = LocalDate.of(2023, 7, 6);
boolean isBefore = date1.isBefore(date2);
System.out.println("Is date1 before date2: " + isBefore);
}
}
Output:
Is date1 before date2: true
32. isAfter(LocalDate otherDate)
Checks if this date is after the specified date.
Example:
import java.time.LocalDate;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2023, 7, 5);
LocalDate date2 = LocalDate.of(2023, 7, 4);
boolean isAfter = date1.isAfter(date2);
System.out.println("Is date1 after date2: " + isAfter);
}
}
Output:
Is date1 after date2: true
33. format(DateTimeFormatter formatter)
Formats this date using the specified formatter.
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateExample {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formattedDate = date.format(formatter);
System.out.println("Formatted date: " + formattedDate);
}
}
Output:
Formatted date: 05/07/2024
Conclusion
The LocalDate
class in Java 8 is used for handling dates without time zones. By using its methods, you can easily create, manipulate, and query dates in a variety of ways. This guide has covered the most common methods, providing you with a solid foundation for working with LocalDate
in your Java applications.
Comments
Post a Comment
Leave Comment