Back to Stream Practice: Employee Analysis
Q22Question 22 of 43

Find all employees who have a birthday this month

Key Concepts
  • LocalDate.now().getMonth() returns the current Month enum value
  • filter(e -> e.getBirthDate().getMonth() == currentMonth) keeps employees born in this month
  • MonthDay.from(birthDate) extracts just the month-day pair, ignoring the year
  • Use Clock injection for testability so 'this month' is deterministic in tests
  • Guard against null birth dates from legacy data with a null check first
  • Group by Month to show birthday counts for every month: groupingBy(e -> e.getBirthDate().getMonth(), counting())

Definition

Filter employees whose birth date's month matches LocalDate.now().getMonth() to find everyone with a birthday this month.
  • LocalDate.now().getMonth() returns the current Month enum value (e.g., Month.JANUARY).
  • filter(e -> e.getBirthDate().getMonth() == currentMonth) compares each employee's birth month to the current month.
  • MonthDay extracts just the month-day pair from a date, useful for birthday comparisons ignoring year.
  • Clock injection makes the current month deterministic in tests.
  • Null safety: guard with e.getBirthDate() != null for legacy data.

The National Bank of India HR team sends birthday greeting cards every month. Priya checks each employee's birth date: if the month matches the current month, that employee gets a card. She does not care about the year, only the month.

Understanding the Concepts

What does this question ask?

The interviewer wants you to filter by a date component (month) extracted from a LocalDate field. This tests Month enum usage, date component extraction, and testability.

How do you find employees with a birthday this month?

JAVA
package com.bank.hr;

import java.time.*;
import java.util.*;

public class BirthdayThisMonth {
    public static void main(String[] args) {
        List<Employee> employees = Employee.getSampleEmployees();
        Month currentMonth = LocalDate.now().getMonth();

        List<Employee> birthdays = employees.stream()
            .filter(e -> e.getBirthDate() != null
                && e.getBirthDate().getMonth() == currentMonth)
            .toList();

        System.out.println("Birthdays in " + currentMonth + ":");
        birthdays.forEach(e ->
            System.out.println("  " + e.getName() + " - "
                + e.getBirthDate()));
    }
}
TEXT
Birthdays in JANUARY:
  Amit - 1989-01-22
  Ananya - 1995-01-05

How do you use MonthDay for exact birthday matching?

JAVA
// Check if today is someone's birthday (exact month + day)
MonthDay today = MonthDay.now();

List<Employee> birthdayToday = employees.stream()
    .filter(e -> e.getBirthDate() != null
        && MonthDay.from(e.getBirthDate()).equals(today))
    .toList();

How do you make it testable?

JAVA
public class BirthdayService {
    private final Clock clock;

    public BirthdayService(Clock clock) {
        this.clock = clock;
    }

    public List<Employee> findBirthdaysThisMonth(List<Employee> employees) {
        Month currentMonth = LocalDate.now(clock).getMonth();
        return employees.stream()
            .filter(e -> e.getBirthDate() != null
                && e.getBirthDate().getMonth() == currentMonth)
            .toList();
    }
}

How do you group employees by birthday month?

JAVA
Map<Month, List<Employee>> byBirthMonth = employees.stream()
    .filter(e -> e.getBirthDate() != null)
    .collect(Collectors.groupingBy(
        e -> e.getBirthDate().getMonth()));

byBirthMonth.forEach((month, emps) ->
    System.out.println(month + ": " + emps.size() + " birthdays"));

What are common mistakes?

Mistake: comparing month as int instead of enum.

JAVA
// WRONG: getMonthValue() returns int, now().getMonth() returns Month enum
int currentMonthInt = LocalDate.now().getMonthValue();  // 1-12
Month currentMonthEnum = LocalDate.now().getMonth();     // JANUARY-DECEMBER

// Both work but be consistent:
// Using enum (preferred):
filter(e -> e.getBirthDate().getMonth() == currentMonthEnum)
// Using int:
filter(e -> e.getBirthDate().getMonthValue() == currentMonthInt)

What are common interview follow-up questions?

  • "How do you find employees with birthdays in the next 7 days?" Compute a date range and check if the birth month-day falls within it.
  • "How do you sort employees by upcoming birthday?" Compare MonthDay values relative to today.
  • "How do you handle February 29 birthdays?" MonthDay.of(2, 29) is valid; check if the current year is a leap year.

Visual Overview

DIAGRAM
  Current month: JANUARY

  Stream<Employee>  (with birth dates)
       │
       │  .filter(birthDate.getMonth() == JANUARY)
       ▼
  ┌──────────────────────────────────────────────┐
  │  Rajesh  1980-03-15  MARCH    ≠ JAN → SKIP  │
  │  Priya   1996-06-20  JUNE     ≠ JAN → SKIP  │
  │  Amit    1989-01-22  JANUARY  = JAN → KEEP   │
  │  Kavita  1985-07-10  JULY     ≠ JAN → SKIP  │
  │  Ananya  1995-01-05  JANUARY  = JAN → KEEP   │
  └──────────────────────────────────────────────┘
       │
       ▼
  [Amit, Ananya]  ← birthdays this month

Key Takeaway

Finding employees with a birthday this month compares each employee's birth date month to the current month. Extract the current month with LocalDate.now().getMonth() and filter with getBirthDate().getMonth() == currentMonth.

MonthDay is useful for exact birthday matching (month + day, ignoring year). MonthDay.from(birthDate).equals(MonthDay.now()) checks if today is someone's birthday.

Always guard against null birth dates and inject a Clock for testability. Group by month to produce a birthday calendar: groupingBy(e -> e.getBirthDate().getMonth()) shows how many employees have birthdays in each month.

Remember one sentence: compare getMonth() enum values to find employees born in the current month, and use MonthDay for exact day-of-month matching.

---

Share
Use ← → arrow keys to navigate