使用 LocalDate 在 2 个特定日期之间打印详细信息

Printing details between 2 specific dates using LocalDate

我正在尝试 return 使用 LocalDate 在数组列表中给定的两个日期之间的数据。我的代码是..

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;
     }

测试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")));

编辑:

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);       
    }
}

我想 return 所有在 2017-03-11 到 2019-07-12 之间入学的学生。 你对此有何建议?这可以用 LocalDate 来完成吗?

您可以使用 isAfter()isBefore() 中的方法 class LocalDate

编辑:

for (Pupil p : pupilList) {
    if (p.enrolDate().isAfter(startDate) && p.enrolDate().isBefore(endDate)) {
        list.add(p);
    }
}

如果您想将 startDate 视为有效的注册日期,只需将条件更改为:

if (p.enrolDate().equal(startDate) || (p.enrolDate().isAfter(startDate) && p.enrolDate().isBefore(endDate)))

您可以使用 LocalDate::isAfter and LocalDate::isBefore 检查 LocalDate 是否介于两个日期之间。

演示

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);
        }
    }
}

输出:

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

如果您想同时包括日期(startDateendDate),您可以按如下方式进行:

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);
}

输出:

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]

首先,School class.

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;
        pupilID = studentID;
        this.joinDate = joinDate;
    }

    public LocalDate getJoinDate() {
        return joinDate;
    }

    public String toString() {
        return name;
    }
}

接下来,MainSchool class.

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class MainSchool {
    /** The list of pupils at the school. */
    private final List<School> pupilList;

    /**
     * Constructor.
     */
    public MainSchool() {
        pupilList = new ArrayList<School>();
    }

    /**
     * Adds newPupil to this school.
     */
    public void addPupil(School newPupil) {
        if (newPupil != null) {
            pupilList.add(newPupil);
        }
    }

    /**
     * Returns a list of students who joined the school between start and end.
     */
    public List<School> getEnrollmentsBetween(LocalDate start, LocalDate end) {
        Objects.requireNonNull(start, "'start' is null");
        Objects.requireNonNull(end, "'end' is null");
        List<School> enrollments;
        if (start.isAfter(end)) {
            enrollments = new ArrayList<School>();
        }
        else {
            enrollments = pupilList.stream()
                                   .filter(pupil -> start.isBefore(pupil.getJoinDate())  &&  end.isAfter(pupil.getJoinDate()))
                                   .collect(Collectors.toList());
        }
        return enrollments;
    }

    /**
     * For testing this class.
     */
    public static void main(String[] args) {
        MainSchool myPupil = new MainSchool();
        myPupil.addPupil(new School("John",   "301B", 8588, LocalDate.parse("2017-03-11")));
        myPupil.addPupil(new School("William","401B", 8589, LocalDate.parse("2018-05-12")));
        myPupil.addPupil(new School("Jessica","501B", 8590, LocalDate.parse("2019-07-12")));
        myPupil.addPupil(new School("Linda",  "601B", 8591, LocalDate.parse("2020-01-10")));
        List<School> enrollments = myPupil.getJoinedBetween(LocalDate.parse("2017-03-11"),
                                                                 LocalDate.parse("2019-07-12"));
        System.out.println(enrollments);
    }
}

方法 getEnrollmentsBetween() 使用 java 的 stream API 方法 Java 8.

LocalDateRange::contains

这里的其他答案都是正确的。

此外,如果您愿意添加 ThreeTen-Extra library to your project, I might suggest using the LocalDateRange class.

此 class 将时间跨度表示为一对 LocalDate 对象。 class 提供了很好的比较方法,例如 containsabutsoverlaps 等。

定义您的目标时间跨度。

LocalDate start = LocalDate.parse( "2017-03-11" ) ;
LocalDate end = LocalDate.parse( "2019-07-12" ) ; 
LocalDateRange range = LocalDateRange.of( start , end ) ;

循环您的业务对象,询问每个 Pupil 对象:

boolean inRange = range.contains( pupil.dateEnroled ) ;