简体   繁体   English

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

[英]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.我正在尝试使用 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测试 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.我想归还所有在 2017 年 3 月 11 日至 2019 年 7 月 12 日期间加入学校的学生。 What would you suggest for this?您对此有何建议? Can this be done with LocalDate?这可以用 LocalDate 完成吗?

You can use LocalDate::isAfter and LocalDate::isBefore to check if a LocalDate falls between two dates.您可以使用LocalDate::isAfterLocalDate::isBefore检查LocalDate是否介于两个日期之间。

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: 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:如果要包括两个日期( 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);
}

Output: 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]

LocalDateRange::contains

Other Answers here are correct.这里的其他答案是正确的。

In addition, if you are willing to add the ThreeTen-Extra library to your project, I might suggest using the LocalDateRange class.此外,如果您愿意将ThreeTen-Extra库添加到您的项目中,我可能会建议使用LocalDateRange class。

This class represents a span-of-time as a pair of LocalDate objects.此 class 将时间跨度表示为一对LocalDate对象。 The class offers nifty methods for comparison, such as contains , abuts , overlaps , and so on. class 提供了漂亮的比较方法,例如containsabutsoverlaps等。

Define your target span-of-time.定义您的目标时间跨度。

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

Loop your business objects, asking for each Pupil object:循环您的业务对象,询问每个瞳孔 object:

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

You can use isAfter() , isBefore() methods from the class LocalDate .您可以使用 class LocalDate中的isAfter()isBefore()方法。

EDIT:编辑:

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

If you want to consider the startDate as a valid date to enrol just change the condition to this:如果您想将startDate视为有效的注册日期,只需将条件更改为:

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

First, the School class.一、 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;
    }
}

Next, the MainSchool class.接下来, 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);
    }
}

Method getEnrollmentsBetween() uses java's stream API that was introduced in Java 8.方法getEnrollmentsBetween()使用在 Java 8 中引入的 java 的stream API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM