Search code examples
javaarrayslistdatelocaldate

Printing details between 2 specific dates using LocalDate


I am trying to return the data between two dates given within an array list using LocalDate. My code is..

import java.time.LocalDate;

public class School {    
     private String name;
     private String classID;
     private int pupilID;
     private LocalDate joinDate;

     public School(String name, String classID, int studentID, LocalDate joinDate)
     {  
        this.name = name; 
        this.classID = classID;
        this.pupilID = pupilID;
        this.enrolDate = joinDate;
     }

Test class

School myPupil = new School();

myPupil.addPupil(new Pupil("John","301B", "8588", LocalDate.parse("2017-03-11")));
myPupil.addPupil(new Pupil("William","401B", "8589", LocalDate.parse("2018-05-12")));
myPupil.addPupil(new Pupil("Jessica","501B", "8590", LocalDate.parse("2019-07-12")));
myPupil.addPupil(new Pupil("Linda","601B", "8591", LocalDate.parse("2020-01-10")));

Edit:

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MainSchool{

    private ArrayList<School> pupilList;

    public MainSchool()
    {
        pupilList = new pupilList<School>();
    }   

    public void addPupil(School newPupil)       
    {
        pupilList.addPupil(newPupil);       
    }
}

I want to return all the pupils that joined the school between 2017-03-11 to 2019-07-12. What would you suggest for this? Can this be done with LocalDate?


Solution

  • You can use LocalDate::isAfter and LocalDate::isBefore to check if a LocalDate falls between two dates.

    Demo

    import java.time.LocalDate;
    import java.util.ArrayList;
    import java.util.List;
    
    class Pupil {
        private String studID;
        private String subjectID;
        private String regiNumber;
        private LocalDate dateEnroled;
    
        public Pupil(String studID, String subjectID, String regiNumber, LocalDate dateEnroled) {
            this.studID = studID;
            this.subjectID = subjectID;
            this.regiNumber = regiNumber;
            this.dateEnroled = dateEnroled;
        }
    
        public LocalDate getDateEnroled() {
            return dateEnroled;
        }
    
        @Override
        public String toString() {
            return "Pupil [studID=" + studID + ", subjectID=" + subjectID + ", regiNumber=" + regiNumber + ", dateEnroled="
                    + dateEnroled + "]";
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            List<Pupil> pupilList = new ArrayList<Pupil>();
            pupilList.add(new Pupil("John", "301B", "8588", LocalDate.parse("2017-03-11")));
            pupilList.add(new Pupil("William", "401B", "8589", LocalDate.parse("2018-05-12")));
            pupilList.add(new Pupil("Jessica", "501B", "8590", LocalDate.parse("2019-07-12")));
            pupilList.add(new Pupil("Linda", "601B", "8591", LocalDate.parse("2020-01-10")));
    
            List<Pupil> list = new ArrayList<Pupil>();
            LocalDate startDate = LocalDate.parse("2017-03-11");
            LocalDate endDate = LocalDate.parse("2019-07-12");
            for (Pupil pupil : pupilList) {
                if (pupil.getDateEnroled().isAfter(startDate) && pupil.getDateEnroled().isBefore(endDate)) {
                    list.add(pupil);
                }
            }
    
            // Display the list
            for (Pupil pupil : list) {
                System.out.println(pupil);
            }
        }
    }
    

    Output:

    Pupil [studID=William, subjectID=401B, regiNumber=8589, dateEnroled=2018-05-12]
    

    If you want to include both the dates (startDate and endDate), you can do it as follows:

    List<Pupil> list = new ArrayList<Pupil>();
    LocalDate startDate = LocalDate.parse("2017-03-11").minusDays(1);
    LocalDate endDate = LocalDate.parse("2019-07-12").plusDays(1);
    for (Pupil pupil : pupilList) {
        if (pupil.getDateEnroled().isAfter(startDate) && pupil.getDateEnroled().isBefore(endDate)) {
            list.add(pupil);
        }
    }
    
    // Display the list
    for (Pupil pupil : list) {
        System.out.println(pupil);
    }
    

    Output:

    Pupil [studID=John, subjectID=301B, regiNumber=8588, dateEnroled=2017-03-11]
    Pupil [studID=William, subjectID=401B, regiNumber=8589, dateEnroled=2018-05-12]
    Pupil [studID=Jessica, subjectID=501B, regiNumber=8590, dateEnroled=2019-07-12]