Java Date and Time API

The java.time package provides the modern Date and Time API introduced in Java 8. It offers immutable and thread-safe classes for working with dates, times, durations, periods, time zones, and timestamps.

LocalDate

LocalDate represents a date without a time or time zone.

Java
Creating and working with LocalDate.
import java.time.LocalDate;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        LocalDate birthday = LocalDate.of(2000, 5, 20);

        System.out.println(today);
        System.out.println(birthday);
        System.out.println(today.getYear());
    }
}

LocalTime

LocalTime represents a time without a date or time zone.

Java
Creating and modifying a LocalTime.
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        LocalTime meeting = LocalTime.of(10, 30);

        System.out.println(now);
        System.out.println(meeting);
        System.out.println(meeting.plusHours(2));
    }
}

LocalDateTime

LocalDateTime combines a date and time without storing time-zone information.

Java
Working with LocalDateTime.
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime appointment = LocalDateTime.of(2026, 9, 24, 14, 30);

        System.out.println(now);
        System.out.println(appointment);
    }
}

Date Arithmetic

The Date and Time API provides methods such as plusDays, minusDays, plusMonths, and minusYears for date calculations.

Java
Adding and subtracting time from a date.
LocalDate date = LocalDate.of(2026, 9, 24);

LocalDate nextWeek = date.plusDays(7);
LocalDate previousMonth = date.minusMonths(1);

System.out.println(nextWeek);
System.out.println(previousMonth);

Period

Period represents a date-based amount of time using years, months, and days.

Java
Calculating the period between two dates.
import java.time.LocalDate;
import java.time.Period;

public class Main {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2020, 1, 1);
        LocalDate end = LocalDate.of(2026, 1, 1);

        Period period = Period.between(start, end);

        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }
}

Duration

Duration represents a time-based amount such as hours, minutes, seconds, and nanoseconds.

Java
Calculating the duration between two times.
import java.time.Duration;
import java.time.LocalTime;

public class Main {
    public static void main(String[] args) {
        LocalTime start = LocalTime.of(10, 0);
        LocalTime end = LocalTime.of(12, 30);

        Duration duration = Duration.between(start, end);

        System.out.println(duration.toHours());
        System.out.println(duration.toMinutes());
    }
}

Formatting Dates

DateTimeFormatter converts date and time objects into readable strings.

Java
Formatting a LocalDate.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(2026, 9, 24);

        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("dd-MM-yyyy");

        System.out.println(date.format(formatter));
    }
}

Parsing Dates

The parse method converts formatted text into date or time objects.

Java
Parsing a string into LocalDate.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter =
                DateTimeFormatter.ofPattern("dd-MM-yyyy");

        LocalDate date = LocalDate.parse("24-09-2026", formatter);

        System.out.println(date);
    }
}

ZonedDateTime

ZonedDateTime represents a date and time together with a time zone.

Java
Creating a date and time for a specific time zone.
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime indiaTime =
                ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));

        ZonedDateTime londonTime =
                ZonedDateTime.now(ZoneId.of("Europe/London"));

        System.out.println(indiaTime);
        System.out.println(londonTime);
    }
}

Instant

Instant represents a point on the UTC timeline. It is useful for timestamps, logging, event ordering, and storing machine-readable time.

Java
Creating a UTC timestamp.
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant timestamp = Instant.now();

        System.out.println(timestamp);
    }
}

Which Date-Time Class Should You Use?

TEXT
Quick guide to the most common java.time classes.
LocalDate      : Date without time zone
LocalTime      : Time without date or zone
LocalDateTime  : Date and time without zone
ZonedDateTime  : Date and time with zone
Instant        : Point on the UTC timeline
Period         : Date-based amount of time
Duration       : Time-based amount of time

Best Practices

Prefer java.time classes over the legacy Date and Calendar APIs for new Java applications. Use LocalDate for calendar dates, Instant for machine timestamps, and ZonedDateTime when time-zone information matters.

Keep date-time values immutable and avoid converting between time zones unless the application actually needs it. Always make the intended time zone explicit when working with users or systems in different regions.

Conclusion

The Java Date and Time API provides a clean and reliable way to work with dates, times, durations, periods, timestamps, and time zones. Understanding the main java.time classes makes date-time operations much safer and easier to maintain.