java - How to print the list of employees who have joined on a particular month from an employee list?

Java - How to print the list of employees who have joined on a particular month from an employee list?

To print the list of employees who have joined on a particular month from an employee list in Java, you can use the java.time package for handling dates and the Stream API for filtering. Here's an example:

Suppose you have an Employee class with attributes including the joining date:

import java.time.LocalDate; public class Employee { private String name; private LocalDate joinDate; // Constructors, getters, and setters... } 

Now, let's assume you have a list of employees and you want to filter employees who joined in a specific month. Here's how you can do it:

import java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class EmployeeFilter { public static void main(String[] args) { // Sample list of employees List<Employee> employeeList = new ArrayList<>(); employeeList.add(new Employee("John", LocalDate.of(2022, Month.JANUARY, 15))); employeeList.add(new Employee("Alice", LocalDate.of(2022, Month.FEBRUARY, 20))); employeeList.add(new Employee("Bob", LocalDate.of(2022, Month.MARCH, 10))); employeeList.add(new Employee("Eve", LocalDate.of(2022, Month.APRIL, 5))); // Specify the target month Month targetMonth = Month.FEBRUARY; // Filter employees who joined in the target month List<Employee> employeesInTargetMonth = filterEmployeesByJoinMonth(employeeList, targetMonth); // Print the result System.out.println("Employees who joined in " + targetMonth + ":"); employeesInTargetMonth.forEach(employee -> System.out.println(employee.getName())); } public static List<Employee> filterEmployeesByJoinMonth(List<Employee> employees, Month targetMonth) { return employees.stream() .filter(employee -> employee.getJoinDate().getMonth() == targetMonth) .collect(Collectors.toList()); } } 

In this example:

  • The EmployeeFilter class has a filterEmployeesByJoinMonth method that takes a list of employees and a target month. It uses the Stream API to filter employees based on the join month.
  • The main method creates a sample list of employees, specifies the target month (e.g., Month.FEBRUARY), and then calls the filtering method.
  • The result is printed to the console.

Adjust the Employee class and the target month as needed for your specific scenario.

Examples

  1. Java code to filter employees who joined in a specific month

    // Using Java 8 streams and LocalDate List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().getMonth() == Month.JANUARY) // Replace with the desired month .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 
  2. How to filter employees by join month in Java using forEach loop

    // Using forEach loop List<Employee> filteredList = new ArrayList<>(); for (Employee employee : employeeList) { if (employee.getJoinDate().getMonth() == Month.FEBRUARY) { // Replace with the desired month filteredList.add(employee); } } // Print the filtered list filteredList.forEach(System.out::println); 
  3. Java code to print employees joined in a specific month using SimpleDateFormat

    // Using SimpleDateFormat List<Employee> filteredList = new ArrayList<>(); SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.ENGLISH); for (Employee employee : employeeList) { String joinMonth = monthFormat.format(employee.getJoinDate()); if (joinMonth.equals("March")) { // Replace with the desired month filteredList.add(employee); } } // Print the filtered list filteredList.forEach(System.out::println); 
  4. How to display employees joined in a particular month in Java with Java 8 LocalDate

    // Using Java 8 LocalDate and DateTimeFormatter DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", Locale.ENGLISH); String targetMonth = "April"; // Replace with the desired month List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().format(monthFormatter).equals(targetMonth)) .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 
  5. Java code to filter and print employees joined in a specific month using HashMap

    // Using HashMap for month mapping Map<Integer, String> monthMap = new HashMap<>(); monthMap.put(1, "January"); monthMap.put(2, "February"); // ... (add entries for all months) List<Employee> filteredList = new ArrayList<>(); int targetMonth = 5; // Replace with the desired month for (Employee employee : employeeList) { if (employee.getJoinDate().getMonthValue() == targetMonth) { filteredList.add(employee); } } // Print the filtered list filteredList.forEach(System.out::println); 
  6. Java code to filter employees by joining month using Java 8 Map and groupingBy

    // Using Java 8 Map and groupingBy Map<Month, List<Employee>> employeesByMonth = employeeList.stream() .collect(Collectors.groupingBy(e -> e.getJoinDate().getMonth())); // Retrieve employees for a specific month List<Employee> filteredList = employeesByMonth.get(Month.JUNE); // Replace with the desired month // Print the filtered list filteredList.forEach(System.out::println); 
  7. Java code to print employees joined in a specific month using Java 8 Filter and Predicate

    // Using Java 8 Filter and Predicate List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().getMonth() == Month.JULY) // Replace with the desired month .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 
  8. How to filter employees by joining month in Java using Java 8 and Month enum

    // Using Java 8 and Month enum List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().getMonth().equals(Month.AUGUST)) // Replace with the desired month .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 
  9. Java code to filter employees joined in a specific month using Java 8 and LocalDateTime

    // Using Java 8 and LocalDateTime List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().getMonthValue() == 9) // Replace with the desired month .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 
  10. How to print employees who joined in a specific month using Java 8 LocalDate and Month enum

    // Using Java 8 LocalDate and Month enum List<Employee> filteredList = employeeList.stream() .filter(e -> e.getJoinDate().getMonth() == Month.OCTOBER) // Replace with the desired month .collect(Collectors.toList()); // Print the filtered list filteredList.forEach(System.out::println); 

More Tags

evaluation gyp centering shadow-dom exacttarget jasmine-node primitive-types vcf-vcard ggplotly centos6.5

More Programming Questions

More Investment Calculators

More Statistics Calculators

More Math Calculators

More Stoichiometry Calculators