LocalDate, LocalTime and LocalDateTime
LocalDate, LocalTime and LocalDateTime are classes in the java.time package that represent dates and times without a time zone. LocalDate is used for a date such as a birthday (2026-11-12). LocalTime is used for a time of day such as an alarm (07:30). LocalDateTime combines both, such as an exam starting at 2026-12-01T09:30. They are immutable and thread-safe: every "change" returns a new object. In this lesson, you will learn how to create, read, change and compare them.
The Three Classes
| Class | Holds | Example | Typical use |
|---|---|---|---|
LocalDate | Year, month, day | 2026-11-12 | Birthdays, due dates, holidays |
LocalTime | Hour, minute, second, nanosecond | 09:30 | Opening hours, alarms |
LocalDateTime | Date and time | 2026-12-01T09:30 | Local events and appointments |
"Local" means "as seen on a wall calendar and clock, with no time zone attached".
Methods
| Method | Description |
|---|---|
now() | It is used to get the current date or time from the system clock. |
of(...) | It creates a specific date or time; months run from 1 to 12. |
parse("2026-11-12") | It creates one from ISO text. |
getYear(), getMonth(), getDayOfMonth(), getDayOfWeek() | It reads parts of a date. |
plusDays(n), minusMonths(n), plusHours(n)... | It returns a new object moved forward or back. |
withDayOfMonth(d), withYear(y)... | It returns a copy with one field changed. |
isBefore, isAfter, isEqual | It compares two values. |
isLeapYear(), lengthOfMonth() | It answers calendar questions. |
atTime(...), toLocalDate(), toLocalTime() | It combines or splits date and time. |
Example: Creating and Reading Dates
Let's see an example with fixed dates, so the output is always the same.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
public class LocalDemo {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 11, 12);
LocalDate exam = LocalDate.parse("2026-12-01");
LocalTime start = LocalTime.of(9, 30);
LocalDateTime examStart = exam.atTime(start);
System.out.println("Today: " + today);
System.out.println("Year " + today.getYear() + ", month "
+ today.getMonth() + ", day " + today.getDayOfMonth());
System.out.println("Day of week: " + today.getDayOfWeek());
System.out.println("Exam starts: " + examStart);
System.out.println("Leap year? " + today.isLeapYear()
+ ", days in February 2028: "
+ LocalDate.of(2028, Month.FEBRUARY, 1).lengthOfMonth());
}
}Today: 2026-11-12
Year 2026, month NOVEMBER, day 12
Day of week: THURSDAY
Exam starts: 2026-12-01T09:30
Leap year? false, days in February 2028: 29Explanation: LocalDate.of(2026, 11, 12) means 12 November: months run from 1 to 12, unlike Calendar. parse reads ISO text (yyyy-MM-dd). atTime joins a date and a time into a LocalDateTime, which prints with a T between them.
Example: Changing and Comparing
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
public class Arithmetic {
public static void main(String[] args) {
LocalDate today = LocalDate.of(2026, 11, 12);
LocalDate due = today.plusDays(30);
System.out.println("Due: " + due);
System.out.println("Last month: " + today.minusMonths(1));
System.out.println("First of month: " + today.withDayOfMonth(1));
System.out.println("Jan 31 + 1 month: "
+ LocalDate.of(2027, 1, 31).plusMonths(1));
today.plusYears(1);
System.out.println("today is still " + today);
System.out.println("Due after today? " + due.isAfter(today));
DayOfWeek day = due.getDayOfWeek();
System.out.println("Weekend? "
+ (day == DayOfWeek.SATURDAY || day == DayOfWeek.SUNDAY));
LocalTime closing = LocalTime.of(17, 0);
System.out.println("Open at 16:45? "
+ LocalTime.of(16, 45).isBefore(closing));
}
}Due: 2026-12-12
Last month: 2026-10-12
First of month: 2026-11-01
Jan 31 + 1 month: 2027-02-28
today is still 2026-11-12
Due after today? true
Weekend? true
Open at 16:45? trueExplanation: plusDays(30) crosses into December correctly. Adding one month to 31 January gives 28 February, the last valid day. Calling today.plusYears(1) without saving the result did nothing, because today is immutable. 12 December 2026 is a Saturday, so the due date falls on a weekend.
Common Mistakes
Calling date.plusDays(1); and ignoring the result. Write date = date.plusDays(1);.
Using LocalDateTime for moments shared across the world. It has no time zone; use ZonedDateTime or Instant.
Comparing dates with ==. Use equals or isEqual.
Interview Corner
What is the difference between LocalDate, LocalTime and LocalDateTime?
All three are immutable java.time classes with no time zone. LocalDate holds only a date (year, month, day), LocalTime only a time of day, and LocalDateTime both together. They're used for values as people read them on a calendar or clock, such as birthdays or local appointments. For an exact moment on the global timeline, use Instant or ZonedDateTime.
Practice
- Easy: Print the date 100 days after 12 November 2026 and its day of the week.
- Harder: Print all the Mondays in December 2026 using a loop with
plusDaysandgetDayOfWeek.
Key Takeaways
Was this tutorial helpful?