Java.Java8.✅ What Is LocalDateTime?

LocalDateTime represents a date and time without a time zone — basically a timestamp like:

2025-04-15T08:30:00

It combines:

  • LocalDate → date (year, month, day)
  • LocalTime → time (hour, minute, second)

🔹 When to Use It

Use LocalDateTime when:

  • You need a full date & time, but
  • You don’t care about time zones or UTC offsets

Perfect for:

  • Logging timestamps
  • Scheduling (e.g., “April 15 at 8:30”)
  • Local clock time (like showing system time)

✅ Example

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Current date & time: " + now);
    }
}

🔹 Output:

Current date & time: 2025-04-15T08:30:12.345

🔧 Common Methods

MethodPurpose
now()Current date & time
of(y, m, d, h, m)Create a specific date-time
plusDays(), minusHours()Add/subtract time
getDayOfWeek()Day of the week (e.g., MONDAY)
toLocalDate()Extract date only
toLocalTime()Extract time only
format(...)Format using DateTimeFormatter

🧠 Bonus: Formatting Example

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

LocalDateTime now = LocalDateTime.now();
String formatted = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
System.out.println(formatted); // e.g. 2025-04-15 08:30
This entry was posted in Без рубрики. Bookmark the permalink.