Skip to content

Latest commit

 

History

History
150 lines (106 loc) · 4.91 KB

File metadata and controls

150 lines (106 loc) · 4.91 KB

Here's the clean, natural, and professional English translation of the entire transcript:


Hey guys, welcome to Code Decode. In this video, we are going to cover everything about the Date-Time API — the questions and interview traps around it in 2025 and beyond.

So let’s get started.

The Date-Time API, introduced in Java 8 and further enhanced through Java 21, provides a robust and modern solution for handling dates and times in applications.

Unlike the legacy classes (java.util.Date and java.util.Calendar), the new API offers many advantages:

  • More type-safe
  • Immutable by design
  • Much clearer and more readable
  • Proper time zone support
  • Better performance

Problems with the Legacy Date-Time API

The old classes like java.util.Date and java.util.Calendar had several serious issues:

  1. Not thread-safe — They were mutable.
  2. Confusing API — Months started from 0 (January = 0, December = 11), not 1 to 12. This caused many bugs.
  3. No clear separation — The same class handled date, time, and timezone information.
  4. Poor readability and error-prone usage.
  5. Weak timezone handling.

Example of mutability issue:

Date today = new Date();
today.setTime(today.getTime() + 1000000);   // Original date gets modified!

This mutability made the legacy API dangerous in multi-threaded environments.


Why the New Java Date-Time API is Better

The modern API (java.time package) solves all these problems.

Main classes you should know:

  • LocalDate → Only date (no time, no zone)
  • LocalTime → Only time (no date, no zone)
  • LocalDateTime → Date + Time (no zone)
  • ZonedDateTime → Date + Time + Timezone
  • Instant → Timestamp (UTC-based, best for storage)
  • Duration → Time-based amount (e.g., 5 hours, 30 minutes)
  • Period → Date-based amount (e.g., 2 years, 3 months)

Key Benefits:

  • Immutable — Once created, the object cannot be changed. Operations return a new object.
  • Thread-safe — Safe to use in multi-threaded environments.
  • Clear separation of concerns.
  • Readable and fluent API.
  • Proper timezone support.
  • Months start from 1 to 12 (no more zero-based confusion).

How to Use the Modern Date-Time API

1. Getting Current Values

LocalDate today = LocalDate.now();           // Only date
LocalTime now = LocalTime.now();             // Only time
LocalDateTime nowWithDateTime = LocalDateTime.now();   // Date + Time
ZonedDateTime zoned = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
Instant instant = Instant.now();             // Best for timestamps

2. Arithmetic Operations (Very Clean)

LocalDate futureDate = LocalDate.now().plusDays(5);
LocalDate lastMonth = LocalDate.now().minusMonths(1);

3. Parsing and Formatting (Thread-safe)

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formatted = LocalDate.now().format(formatter);

LocalDate parsed = LocalDate.parse("15-08-2025", formatter);

4. Working with Timezones

Always store timestamps in UTC (Instant) in the database for consistency.

To display to the user, convert to their local timezone.

Example:

ZonedDateTime istTime = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
ZonedDateTime utcTime = istTime.withZoneSameInstant(ZoneId.of("UTC"));

Best Practices (Very Important for Interviews)

  1. Stop using legacy java.util.Date and java.util.Calendar.
  2. Choose the right class for your use case:
    • Only date → LocalDate (e.g., birth dates)
    • Only time → LocalTime (e.g., business hours)
    • Date + Time → LocalDateTime
    • With timezone → ZonedDateTime
    • Timestamp for storage → Instant
  3. Always store timestamps in UTC in the database.
  4. Convert to local timezone only when displaying to the user.
  5. Cache DateTimeFormatter objects — they are expensive to create.
  6. Handle parsing exceptions gracefully (DateTimeParseException).

Summary

The legacy Date-Time API was:

  • Mutable
  • Not thread-safe
  • Confusing (zero-based months)
  • Poorly designed

The new java.time API is:

  • Immutable
  • Thread-safe
  • Clear and readable
  • Properly supports timezones
  • Much more performant

Rule of thumb:

  • Use LocalDate for dates only.
  • Use LocalTime for time only.
  • Use Instant for storing timestamps.
  • Use ZonedDateTime when timezone matters.

If you want a Part 2 covering advanced topics like Clock injection, proper comparison methods, handling daylight saving, etc., just let me know in the comments.

Thank you!


(The video also includes the usual course promotion for the Angular + Spring Boot Microservices full-stack course, which has been omitted here as it is repetitive.)


Would you like a quick cheat sheet summarizing all the key classes (LocalDate, LocalTime, Instant, etc.) with their use cases? It’s very useful for interviews.