Java Localdate Class
π
2026-06-22 | π Java
Java LocalDate Class | Rookie Tutorial\n\nJava LocalDate Class
\n\nLocalDate is an immutable date class introduced in Java 8, part of the `java.time` package.\n\nLocalDate represents a date without timezone (year-month-day), such as "2023-05-15". LocalDate does not contain time and timezone information, making it very suitable for scenarios that only require dates, such as birthdays, holidays, etc.\n\n* * *\n\n## Main Features of LocalDate\n\n### 1. Immutability\n\nLocalDate instances are immutable; any modification operation returns a new LocalDate object instead of modifying the original object.\n\n### 2. Thread Safety\n\nDue to immutability, LocalDate is thread-safe and can be safely used in multi-threaded environments.\n\n### 3. ISO-8601 Standard\n\nLocalDate follows the ISO-8601 calendar system, which is the international standard for modern date and time representation.\n\n* * *\n\n## Creating LocalDate Objects\n\n### 1. Get Current Date\n\nLocalDate today = LocalDate.now();System.out.println("Today's date: " + today);\n### 2. Create with Specific Date\n\nLocalDate specificDate = LocalDate.of(2023, Month.MAY, 15);// orLocalDate specificDate2 = LocalDate.of(2023, 5, 15);\n### 3. Parse from String\n\nLocalDate parsedDate = LocalDate.parse("2023-05-15");\n\n* * *\n\n## Common Operation Methods\n\n### 1. Get Date Information\n\n## Example\n\nLocalDate date = LocalDate.of(2023, 5, 15);\n\nint year = date.getYear();// 2023\n\n Month month = date.getMonth();// MAY\n\nint day = date.getDayOfMonth();// 15\n\n DayOfWeek dow = date.getDayOfWeek();// MONDAY\n\nint len = date.lengthOfMonth();// 31 (number of days in May)\n\nboolean leap = date.isLeapYear();// false (2023 is not a leap year)\n\n### 2. Date Addition and Subtraction\n\n## Example\n\nLocalDate tomorrow = today.plusDays(1);\n\n LocalDate nextWeek = today.plusWeeks(1);\n\n LocalDate nextMonth = today.plusMonths(1);\n\n LocalDate nextYear = today.plusYears(1);\n\n LocalDate yesterday = today.minusDays(1);\n\n### 3. Date Comparison\n\n## Example\n\nLocalDate date1 = LocalDate.of(2023, 5, 15);\n\n LocalDate date2 = LocalDate.of(2023, 6, 20);\n\nboolean isBefore = date1.isBefore(date2);// true\n\nboolean isAfter = date1.isAfter(date2);// false\n\nboolean isEqual = date1.isEqual(date2);// false\n\n### 4. Adjust Date\n\n## Example\n\nLocalDate date = LocalDate.of(2023, 5, 15);\n\n LocalDate firstDayOfMonth = date.withDayOfMonth(1);\n\n LocalDate lastDayOfYear = ΠΊΡΠ°ΠΉDate.with(TemporalAdjusters.lastDayOfYear());\n\n* * *\n\n## Formatting and Parsing\n\n### 1. Format Date\n\n## Example\n\nLocalDate date = LocalDate.now();\n\n DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");\n\nString formattedDate = date.format(formatter);\n\n### 2. Parse String to Date\n\n## Example\n\nString dateStr ="2023/05/15";\n\n DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");\n\n LocalDate date = LocalDate.parse(dateStr, formatter);\n\n* * *\n\n## Practical Application Examples\n\n### 1. Calculate Days Between Two Dates\n\n## Example\n\nLocalDate startDate = LocalExtreme.of(2023, 5, 1);\n\n LocalDate endDate = LocalDate.of(2023, 5, 15);\n\nlong daysBetween = ChronoUnit.DAYS.between(startDate, endDate);\n\nSystem.out.println("Days difference: "+ daysBetween);\n\n### 2. Check if Date is Within Range\n\n## Example\n\nLocalDate dateToCheck = LocalDate.of(2023, 5, 10);\n\n LocalDate startDate = LocalDate.of(2023, 5, 1);\n\n LocalDate endDate = LocalDate.of(2023, 5, 31);\n\nif(dateToCheck.isAfter(startDate)&& dateToCheck.isBefore(endDate)){\n\nSystem.out.println("Date is within range");\n\n}\n\n### 3. Get All Dates of a Month\n\n## Example\n\nLocalDate date = LocalDate.of(2023, 5, 1);\n\nint daysInMonth = date.lengthOfMonth();\n\nfor(int i =1; i <= daysInMonth; i++){\n\nSystem.out.println(date.withDayOfMonth(i));\n\n}\n\n### Comprehensive Example\n\n## Example\n\nimport java.time.LocalDate;\n\nimport java.time.Month;\n\nimport java.time.DayOfWeek;\n\nimport java.time.format.DateTimeFormatter;\n\nimport java.time.temporal.ChronoUnit;\n\nimport java.util.Locale;\n\npublic class LocalDateDemo {\n\npublic static void main(String[] args){\n\n// 1. Create LocalDate instances\n\nSystem.out.println("=== Create LocalDate instances ===");\n\n LocalDate today = LocalDate.now();\n\n LocalDate specificDate = LocalDate.of(2023, Month.JULY, 15);\n\n LocalDate parsedDate = LocalDate.parse("2023-08-20");\n\nSystem.out.println("Current date: "+ today);\n\nSystem.out.println("Specific date: "+ specificDate);\n\nSystem.out.println("Parsed date: "+ parsedDate);\n\n// 2. Get date information\n\nSystem.out.println("\\n=== Get date information ===");\n\nSystem.out.println("Year: "+ today.getYear());\n\nSystem.out.println("Month: "+ today.getMonth()+" ("+ today.getMonthValue()+")");\n\nSystem.out.println("Day: "+ today.getExtremeOfMonth());\n\nSystem.out.println("Weekday: "+ today.getDayOfWeek());\n\nSystem.out.println("Day of year: "+ today.getDayOfYear());\n\nSystem.out.println("Days in month: "+ today.lengthOfMonth());\n\nSystem.out.println("Days in year: "+ today.lengthOfYear());\n\nSystem.out.println("Is leap year: "+ today.isLeapYear());\n\n// 3. Date calculation\n\nSystem.out.println("\\n=== Date calculation ===");\n\nSystem.out.println("Add 5 days: "+ today.plusDays(5));\n\nSystem.out.println("Add 2 weeks: "+ today.plusWeeks(2));\n\nSystem.out.println("Add 3 months: "+ today.plusMonths(3));\n\nSystem.out.println("Subtract 1 year: "+ today.minusYears(1));\n\nSystemExtreme.println("First day of next month: "+ today.plusMonths(1).withDayOfMonth(1));\n\nSystem.out.println("Last day of this month: "+ today.withDayOfMonth(today.lengthOfMonth()));\n\n// 4. Date comparison\n\nSystem.out.println("\\n=== Date comparison ===");\n\n LocalDate date1 = LocalDate.of(2023, 6, 15);\n\n LocalDate date2 = LocalDate.of(2023, 6, 20);\n\nSystem.out.println(date1 +" before "+ date2 +"? "+ date1.isBefore(date2));\n\nSystem.out.println(date1 +" after "+ date2 +"? "+ date1.isAfter(date2));\n\nSystem.out.println(date1 +" equals "+ date2 +"? "+ date1.isEqual(date2));\n\n// 5. Calculate date difference\n\nSystem.out.println("\\n=== Calculate date difference ===");\n\n LocalDate startDate = LocalDate.of(2023, 1, 1);\n\n LocalDate endDate = LocalDate.of(2023, 12, 31);\n\nSystem.out.println("Days between dates: "+ ChronoUnit.DAYS.between(startDate, endDate));\n\nSystem.out.println("Months between dates: "+ ChronoUnit.MONTHS.between(startDate, endDate));\n\nSystem.out.println("Years between dates: "+ ChronoUnit.YEARS.between(startDate, endDate));\n\n// 6. Date formatting\n\nSystem.out.println("\\n=== Date formatting ===");\n\n DateTimeFormatter basicFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");\n\n DateTimeFormatter chineseFormatter = DateTimeFormatter.ofPattern("yyyyYearMMMonthddDayEEEE", Locale.CHINA);\n\n DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyy", Locale.US);\n\nSystem.out.println("Basic format: "+ today.format(basicFormatter));\n\nSystem.out.println("Chinese format: "+ today.format(chineseFormatter));\n\nSystem.out.println("English format: "+ today.format(customFormatter));\n\n// 7. Other utility methods\n\nSystem.out.println("\\n=== Other utility methods ===");\n\nSystem.out.println("Adjust to next Monday: "+ today.with(DayOfWeek.MONDAY));\n\nSystem.out.println("Is weekend: "+\n\n(today.getDayOfWeek()== DayOfWeek.SATURDAY|| today.getDayOfWeek()== DayOfWeek.SUNDAY));\n\n// 8. Date range iteration\n\nSystem.out.println("\\n=== Date range iteration ===");\n\nSystem.out.println("Next 7 days:");\n\n today.datesUntil(today.plusDays(7))\n\n.forEach(System.out::println);\n\n}\n\n}\n\nOutput similar to:\n\n=== Create LocalDate instances ===Current date: 2023-06-15Specific date: 2023-07-15Parsed date: 2023-08-20=== Get date information ===Year: 2023Month: JUNE (6)Day: 15Weekday: THURSDAY Day of year: 166Days in month: 30Days in year: 365Is leap year: false=== Date calculation ===Add 5 days: 2023-06-20Add 2 weeks: 2023-06-29Add 3 months: 2023-09-15Subtract 1 year: 2022-06-15First day of next month: 2023-07-01Last day of this month: 2023-06-30=== Date comparison ===2023-06-15 before 2023-06-20? true2023-06-15 after 2023-06-20? false2023-06-15 equals 2023-06-20? false=== Calculate date difference ===Days between dates: 364Months between dates: 11Years between dates: 0=== Date formatting ===Basic format: 2023/06/15Chinese format: 2023Year06Month15Day ThursdayEnglish format: Jun 15, 2023=== Other utility methods ===Adjust to next Monday: 2023-06-19Is weekend: false=== Date range iteration ===Next 7 days:2023-06-152023-06-162023-06-172023-06-182023-Extreme-192023-06-202023-06-21\n\n* * *\n\n## Summary\n\nThe LocalDate class is an important part of the Java 8 date and time API, providing rich methods for handling date-related operations. Compared to the old Date and Calendar classes, LocalDate is more intuitive, easy to use, and thread-safe. Mastering the use of LocalDate can greatly improve the efficiency of date processing and the readability of code.\n\nHere are its common methods:\n\n### Static Factory Methods\n\n| Method | Description | Example |\n| --- | --- | --- |\n| `static LocalDate now()` | Get current date | `LocalDate.now()` |\n| `static LocalDate of(int year, int month, int dayOfMonth)` | Create with specified year, month, day | `LocalDate.of(2023, 6, 15)` |\n| `static LocalDate of(int year, Month month, int dayOfMonth)` | Create with specified year, month, day (using Month enum) | `LocalDate.of(2023, Month.JUNE, 15)` |\n| `static LocalDate parse(CharSequence text)` | Parse from string (ISO format: yyyy-MM-dd) | `LocalDate.parse("2023-06-15")` |\n| `static LocalDate parse(CharSequence text, DateTimeFormatter formatter)` | Parse using specified format | `LocalDate.parse("15/06/2023", DateTimeFormatter.ofPattern("dd/MM/yyyy"))` |\n\n### Get Date Information\n\n| Method | Description | Example |\n| --- | --- | --- |\n| `int getYear()` | Get year | `2023` |\n| `Month getMonth()` | Get month (Month enum) | `Month.JUNE` |\n| `int getMonthValue()` | Get month value (1-12) | `6` |\n| `int getDayOfMonth()` | Get day of month | `15` |\n| `int getDayOfYear()` | Get day of year | `166` |\n| `DayOfWeek getDayOfWeek()` | Get day of week (DayOfWeek enum) | `DayOfWeek.THURSDAY` |\n| `int lengthOfMonth()` | Return number of days in month | `30` |\n| `int lengthOfYear()` |