Return to site

☕⏱️ In JAVA DateTime, is the YOUNGEST the MAX or the MIN?

A later birth date means a younger person. So: max() or min()? 🤔

· java

🔸 TLDR

For dates representing birthdays:

▪️ min() → oldest person 👴

  1. ▪️ max() → youngest person 👶

Why?

Because Java compares dates chronologically:

PAST ←────────────→ FUTURE older younger

So the most recent date is the MAX. ✅

Let's check several Java date/time classes.

Section image

🔸 JAVA.UTIL.DATE

List<Date> dates = List.of(
    Date.from(Instant.parse("1985-01-01T00:00:00Z")),
    Date.from(Instant.parse("1995-01-01T00:00:00Z")),
    Date.from(Instant.parse("2005-01-01T00:00:00Z"))
);

Date youngest = dates.stream()
    .max(Comparator.naturalOrder())
    .orElseThrow();

System.out.println(youngest);

✅ 2005 is the youngest.

Date natural ordering goes from earlier to later.

#Date

🔸 JAVA.TIME.LOCALDATE

List<LocalDate> dates = List.of(
    LocalDate.of(1985, 1, 1),
    LocalDate.of(1995, 1, 1),
    LocalDate.of(2005, 1, 1)
);

LocalDate youngest = dates.stream()
    .max(Comparator.naturalOrder())
    .orElseThrow();

System.out.println(youngest);

✅ 2005-01-01

Perfect for birthdays because LocalDate represents a date without a time or timezone.

#LocalDate

🔸 JAVA.TIME.LOCALDATETIME

List<LocalDateTime> dates = List.of(
    LocalDateTime.of(1985, 1, 1, 10, 0),
    LocalDateTime.of(1995, 1, 1, 10, 0),
    LocalDateTime.of(2005, 1, 1, 10, 0)
);

LocalDateTime youngest = dates.stream()
    .max(Comparator.naturalOrder())
    .orElseThrow();

System.out.println(youngest);

✅ 2005-01-01T10:00

Again: later date-time = greater value = MAX.

#LocalDateTime

🔸 JAVA.TIME.ZONEDDATETIME

ZonedDateTime represents a date and time with a time zone 🌍

List<ZonedDateTime> dates = List.of(
    ZonedDateTime.of(
        1985, 1, 1, 10, 0, 0, 0,
        ZoneId.of("Europe/Paris")
    ),
    ZonedDateTime.of(
        1995, 1, 1, 10, 0, 0, 0,
        ZoneId.of("America/New_York")
    ),
    ZonedDateTime.of(
        2005, 1, 1, 10, 0, 0, 0,
        ZoneId.of("Asia/Tokyo")
    )
);

ZonedDateTime youngest = dates.stream()
    .max(Comparator.comparing(ZonedDateTime::toInstant))
    .orElseThrow();

System.out.println(youngest);

✅ The 2005 value is the youngest.

Unlike LocalDateTime, a ZonedDateTime can be mapped to an exact point on the global timeline.

That matters because:

2026-09-17T10:00 Europe/Paris

and

2026-09-17T10:00 Asia/Tokyo

have the same local clock time, but they do not represent the same instant.

Using:

Comparator.comparing(ZonedDateTime::toInstant)

makes the intent explicit:

▪️ Earlier instant → smaller

▪️ Later instant → greater

▪️ Latest instant → MAX → youngest/newest

#ZonedDateTime

🔸 JAVA.TIME.INSTANT

List<Instant> dates = List.of(
    Instant.parse("1985-01-01T00:00:00Z"),
    Instant.parse("1995-01-01T00:00:00Z"),
    Instant.parse("2005-01-01T00:00:00Z")
);

Instant youngest = dates.stream()
    .max(Comparator.naturalOrder())
    .orElseThrow();

System.out.println(youngest);

✅ 2005-01-01T00:00:00Z

Instant represents an actual point on the UTC timeline.

Later instant → greater value → MAX.

#Instant

🔸 TAKEAWAYS

▪️ Earliest date = MIN

▪️ Latest date = MAX

▪️ Oldest birthday = MIN

▪️ Youngest birthday = MAX

▪️ Date, LocalDate, LocalDateTime and Instant follow chronological ordering

▪️ Date, LocalDate, LocalDateTime, ZonedDateTime and Instant can all be used to find the latest value with max(), but ZonedDateTime lets you compare actual moments across time zones.

▪️ When in doubt, think: later on the timeline = greater

So if somebody asks:

"Find the youngest person."

Your Stream probably wants:

.max(Comparator.naturalOrder())

Not min(). 🎯

#Java #Java27 #DateTime #JavaTime #Streams #Comparator #LocalDate #LocalDateTime #Instant #TimeZone #JavaDeveloper #Programming #JavaCertification #OCPJava

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇