LocalDateTime

java.time.LocalDateTime represents a date and time without a time zone. It is a combination of LocalDate and LocalTime.

Commonly Used Methods

Simple Program

import java.time.LocalDateTime;

public class SimpleLocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime deadline = LocalDateTime.of(2025, 7, 20, 18, 0);

        System.out.println("Current date and time: " + now);
        System.out.println("Submission deadline: " + deadline);
        System.out.println("Is deadline after now? " + deadline.isAfter(now));
    }
}

Problem Statement

LotusJavaPrince and Mahesh are building a meeting reminder system for employees. Each reminder must be scheduled precisely with date and time. The system should:

  • Take input for meeting date and time.
  • Validate that the meeting is not in the past.
  • If valid, schedule the meeting.
  • If invalid, display a warning.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class MeetingReminderSystem {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        // Current date-time
        LocalDateTime currentDateTime = LocalDateTime.now();

        // Input date-time
        System.out.print("Enter meeting date and time (yyyy-MM-dd HH:mm): ");
        String input = scanner.nextLine();

        try {
            LocalDateTime meetingTime = LocalDateTime.parse(input, formatter);

            System.out.println("Current time: " + currentDateTime.format(formatter));
            System.out.println("Requested meeting time: " + meetingTime.format(formatter));

            if (meetingTime.isBefore(currentDateTime)) {
                System.out.println("Error: Cannot schedule a meeting in the past.");
            } else {
                System.out.println("Meeting scheduled successfully.");
            }
        } catch (Exception e) {
            System.out.println("Invalid format. Please enter in 'yyyy-MM-dd HH:mm' format.");
        }

        scanner.close();
    }
}
/*
Enter meeting date and time (yyyy-MM-dd HH:mm): 2025-05-22 14:30
Current time: 2025-05-22 11:45
Requested meeting time: 2025-05-22 14:30
Meeting scheduled successfully.
*/

The LocalDateTime class is a powerful tool when you need both date and time together, such as:

  • Scheduling events
  • Logging timestamps
  • Creating calendar applications
  • Managing time-sensitive records

However, it does not include time-zone information, making it unsuitable for representing exact moments in a global context. Use ZonedDateTime if time zone matters.

LocalDateTime offers seamless integration with DateTimeFormatter, LocalDate, and LocalTime, making it ideal for most business-level scheduling and recordkeeping needs.

Scroll to Top