CodeGym /Courses /JAVA 25 SELF /LocalDate, LocalTime, LocalDateTime

LocalDate, LocalTime, LocalDateTime

JAVA 25 SELF
Level 13 , Lesson 2
Available

1. LocalDate: date without time or time zone

LocalDate is just a date. No time, no time zone, no extra hassle. Like a page in a calendar: 2025-06-01 — that’s it. If you don’t care when exactly “midnight” occurs in different cities and you just need “June 1, 2025”, use LocalDate.

How to create LocalDate?

There are several ways, all of them simple and convenient.

Current date

import java.time.LocalDate;

LocalDate today = LocalDate.now();
System.out.println(today); // For example, 2025-06-01

Specific date

LocalDate birthday = LocalDate.of(1990, 12, 15);
System.out.println(birthday); // 1990-12-15

You can use numbers for the year, month, and day. The month is a number from 1 to 12 (January — 1, December — 12).

Parsing from a string

LocalDate parsedDate = LocalDate.parse("2025-06-01");
System.out.println(parsedDate); // 2025-06-01

The string must be in the yyyy-MM-dd format. If the format doesn’t match, an exception will be thrown.

Getting date components

LocalDate has methods that return individual parts of the date:

int year = today.getYear();           // 2025
int month = today.getMonthValue();    // 6 (June)
int day = today.getDayOfMonth();      // 1
System.out.println("Year: " + year + ", month: " + month + ", day: " + day);

If you want the month in words, use the getMonth() method:

System.out.println(today.getMonth()); // JUNE

And if you need the day of the week — the getDayOfWeek() method:

System.out.println(today.getDayOfWeek()); // SATURDAY

Practical example

Let’s continue developing your learning console application: now it can congratulate the user on their birthday.

import java.time.LocalDate;

public class BirthdayApp {
    public static void main(String[] args) {
        LocalDate birthday = LocalDate.of(2000, 2, 29);
        System.out.println("Birth date: " + birthday);
        System.out.println("Day of week of birth: " + birthday.getDayOfWeek());
    }
}

2. LocalTime: time without date or time zone

LocalTime is just time. Hours, minutes, seconds (and even nanoseconds). But no date! If you need to store, for example, “store opening time” or “meeting start time” without tying it to a date — this is your class.

How to create LocalTime?

Current time

import java.time.LocalTime;

LocalTime now = LocalTime.now();
System.out.println(now); // For example, 14:37:12.123456789

Specific time

LocalTime lunchTime = LocalTime.of(13, 30); // 13:30:00
System.out.println(lunchTime);

You can add seconds and nanoseconds:

LocalTime precise = LocalTime.of(8, 15, 30, 123_000_000); // 08:15:30.123
System.out.println(precise);

Parsing from a string

LocalTime parsedTime = LocalTime.parse("14:30:00");
System.out.println(parsedTime); // 14:30

Getting time components

int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();
System.out.println("Hours: " + hour + ", minutes: " + minute + ", seconds: " + second);

Practical example

Let’s add a “clock” to our app:

import java.time.LocalTime;

public class ClockApp {
    public static void main(String[] args) {
        LocalTime current = LocalTime.now();
        System.out.println("Now: " + current);
        System.out.println("Hour: " + current.getHour());
        System.out.println("Minute: " + current.getMinute());
    }
}

3. LocalDateTime: date and time without a time zone

LocalDateTime is a combo: date + time, but still without a time zone. For example, “2025-06-01 14:30:00”. This is convenient when you want to store a moment in time but don’t care where on the planet it will be read.

How to create LocalDateTime?

Current date and time

import java.time.LocalDateTime;

LocalDateTime now = LocalDateTime.now();
System.out.println(now); // For example, 2025-06-01T14:30:15.123456789

Specific date and time

LocalDateTime meeting = LocalDateTime.of(2025, 6, 1, 14, 30);
System.out.println(meeting); // 2025-06-01T14:30

You can add seconds and nanoseconds:

LocalDateTime preciseMeeting = LocalDateTime.of(2025, 6, 1, 14, 30, 45, 123_000_000);
System.out.println(preciseMeeting); // 2025-06-01T14:30:45.123

Parsing from a string

LocalDateTime parsed = LocalDateTime.parse("2025-06-01T14:30:00");
System.out.println(parsed); // 2025-06-01T14:30

Note: the letter T separates the date from the time according to the ISO standard.

Getting components

int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute();
System.out.println("Date: " + year + "-" + month + "-" + day + " Time: " + hour + ":" + minute);

4. Typical operations with date and time

Now that we can create date and time objects, let’s learn to do something useful with them.

Addition and subtraction

All three classes (LocalDate, LocalTime, LocalDateTime) are immutable. This means methods like plusDays() or minusMonths() return a new object without changing the original.

LocalDate

LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
LocalDate lastMonth = today.minusMonths(1);
System.out.println("Today: " + today);
System.out.println("Tomorrow: " + tomorrow);
System.out.println("A month ago: " + lastMonth);

LocalTime

LocalTime now = LocalTime.now();
LocalTime inTenMinutes = now.plusMinutes(10);
System.out.println("Now: " + now);
System.out.println("In 10 minutes: " + inTenMinutes);

LocalDateTime

LocalDateTime start = LocalDateTime.of(2025, 6, 1, 14, 0);
LocalDateTime end = start.plusHours(2).minusMinutes(15);
System.out.println("Start: " + start);
System.out.println("End: " + end);

Comparing dates and times

All classes have the isBefore(), isAfter(), and isEqual() methods:

LocalDate birthday = LocalDate.of(2000, 2, 29);
LocalDate today = LocalDate.now();

if (today.isAfter(birthday)) {
    System.out.println("You are older than a newborn!");
}
LocalTime morning = LocalTime.of(8, 0);
LocalTime now = LocalTime.now();

if (now.isBefore(morning)) {
    System.out.println("It's too early to get up...");
} else {
    System.out.println("Time to wake up!");
}

Getting the current date/time

As you’ve already seen, the now() methods return current values:

LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();

5. Practice: examples and exercises

Example 1: Day of the week for any date

import java.time.LocalDate;

public class DayOfWeekApp {
    public static void main(String[] args) {
        LocalDate anyDate = LocalDate.of(2025, 12, 31);
        System.out.println("December 31, 2025 is " + anyDate.getDayOfWeek());
    }
}

Example 2: How many days until New Year?

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysToNewYear {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate newYear = LocalDate.of(today.getYear() + 1, 1, 1);
        long daysLeft = ChronoUnit.DAYS.between(today, newYear);
        System.out.println("There are " + daysLeft + " days left until New Year!");
    }
}

Example 3: Check whether a year is leap

import java.time.LocalDate;

public class LeapYearCheck {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2025, 1, 1);
        if (date.isLeapYear()) {
            System.out.println(date.getYear() + " is a leap year!");
        } else {
            System.out.println(date.getYear() + " is not a leap year.");
        }
    }
}

Example 4: Difference between two times

import java.time.LocalTime;
import java.time.Duration;

public class TimeDifference {
    public static void main(String[] args) {
        LocalTime start = LocalTime.of(9, 0);
        LocalTime end = LocalTime.of(17, 30);
        Duration duration = Duration.between(start, end);
        System.out.println("The workday lasts " + duration.toHours() + " hours " +
                (duration.toMinutes() % 60) + " minutes.");
    }
}

6. Common mistakes

Mistake No. 1: Confusing LocalDateTime and ZonedDateTime.
LocalDateTime does not contain time zone information! If you’re storing flight schedules between countries or events that happen in different zones — use ZonedDateTime, otherwise you can get unexpected results when moving data between systems.

Mistake No. 2: Using the wrong string format for parsing.
For example, trying to parse 01.06.2025 with LocalDate.parse(). By default, the format 2025-06-01 is expected. For other formats you need a specific DateTimeFormatter (more on that in the next lecture!).

Mistake No. 3: Thinking that methods like plusDays() mutate the object.
No! All objects are immutable. The method returns a new object, and the old one remains unchanged.

Mistake No. 4: Using LocalTime to store an absolute point in time.
LocalTime is only for local time of day. If you want to store a “moment in time”, use Instant or ZonedDateTime.

Mistake No. 5: Forgetting that months are numbered (1 — January, 12 — December).
In LocalDate.of(year, month, day) the month is 1-based (1), not 0 as in some other languages or older APIs.

1
Task
JAVA 25 SELF, level 13, lesson 2
Locked
Magic Clock: displaying the exact time 🕰️
Magic Clock: displaying the exact time 🕰️
1
Task
JAVA 25 SELF, level 13, lesson 2
Locked
Astrological forecast: Birthday and day of week 🌟
Astrological forecast: Birthday and day of week 🌟
1
Task
JAVA 25 SELF, level 13, lesson 2
Locked
Meeting schedule: who is earlier? ⏰
Meeting schedule: who is earlier? ⏰
1
Task
JAVA 25 SELF, level 13, lesson 2
Locked
Secret Mission Time Adjustment 🚀
Secret Mission Time Adjustment 🚀
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION