java.time

java.time is Java’s modern date and time API, introduced in Java 8 to replace the older java.util.Date and java.util.Calendar classes. It’s part of the Java Time API (JSR-310) and is more consistent, immutable, and thread-safe. Let me walk you through the main classes with examples.


1. LocalDate – Date without time

Represents a date (year, month, day) without time and timezone.

import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Today: " + today);

        LocalDate birthday = LocalDate.of(1990, 5, 15);
        System.out.println("Birthday: " + birthday);

        LocalDate tomorrow = today.plusDays(1);
        System.out.println("Tomorrow: " + tomorrow);

        LocalDate lastYear = today.minusYears(1);
        System.out.println("Last year: " + lastYear);
    }
}

Output example:

Today: 2025-08-18
Birthday: 1990-05-15
Tomorrow: 2025-08-19
Last year: 2024-08-18

2. LocalTime – Time without date

Represents time of day (hour, minute, second, nanosecond).

import java.time.LocalTime;

public class LocalTimeExample {
    public static void main(String[] args) {
        LocalTime now = LocalTime.now();
        System.out.println("Now: " + now);

        LocalTime specificTime = LocalTime.of(14, 30, 15);
        System.out.println("Specific Time: " + specificTime);

        LocalTime plusHours = now.plusHours(2);
        System.out.println("Two hours later: " + plusHours);
    }
}

Output example:

Now: 15:42:10.123
Specific Time: 14:30:15
Two hours later: 17:42:10.123

3. LocalDateTime – Date and time without timezone

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Now: " + now);

        LocalDateTime meeting = LocalDateTime.of(2025, 8, 20, 10, 30);
        System.out.println("Meeting: " + meeting);

        LocalDateTime plusDaysHours = now.plusDays(1).plusHours(3);
        System.out.println("Plus 1 day 3 hours: " + plusDaysHours);
    }
}

Output example:

Now: 2025-08-18T15:42:10.123
Meeting: 2025-08-20T10:30
Plus 1 day 3 hours: 2025-08-19T18:42:10.123

4. ZonedDateTime – Date and time with timezone

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedDateTimeExample {
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println("Now: " + now);

        ZonedDateTime tokyoTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
        System.out.println("Tokyo Time: " + tokyoTime);
    }
}

Output example:

Now: 2025-08-18T15:42:10.123+05:30[Asia/Kolkata]
Tokyo Time: 2025-08-18T19:12:10.123+09:00[Asia/Tokyo]

5. Period and Duration – Difference between dates/times

import java.time.LocalDate;
import java.time.Period;
import java.time.LocalTime;
import java.time.Duration;

public class PeriodDurationExample {
    public static void main(String[] args) {
        LocalDate start = LocalDate.of(2020, 1, 1);
        LocalDate end = LocalDate.of(2025, 8, 18);
        Period period = Period.between(start, end);
        System.out.println("Period: " + period.getYears() + " years, " 
                           + period.getMonths() + " months, " 
                           + period.getDays() + " days");

        LocalTime t1 = LocalTime.of(10, 30);
        LocalTime t2 = LocalTime.of(15, 45);
        Duration duration = Duration.between(t1, t2);
        System.out.println("Duration: " + duration.toHours() + " hours, " 
                           + duration.toMinutesPart() + " minutes");
    }
}

Output example:

Period: 5 years, 7 months, 17 days
Duration: 5 hours, 15 minutes

6. Formatting and Parsing

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class FormatParseExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");

        String formattedDate = now.format(formatter);
        System.out.println("Formatted: " + formattedDate);

        LocalDateTime parsedDate = LocalDateTime.parse(formattedDate, formatter);
        System.out.println("Parsed: " + parsedDate);
    }
}

Output example:

Formatted: 18-08-2025 15:42:10
Parsed: 2025-08-18T15:42:10

Key points about java.time:

  • Immutable & thread-safe.
  • Clear separation between date, time, datetime, timezone.
  • Easier arithmetic with plus and minus.
  • Easy formatting and parsing with DateTimeFormatter.

Leave a Reply