说明
根据小孩的出生日期判断是否可以入学。
源码
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class LambdaDemo {
public static void main(String[] args) {
List<Student> list = getInitStudentList();
LambdaDemo ld = new LambdaDemo();
list.forEach(s -> ld.ifEnterSchool(s));
}
private void ifEnterSchool(Student s){
Date baseTime = getDate("2012-9-1");
if(s.getBirthday().getTime() > baseTime.getTime()){
System.out.println(s.getName() + "'s age is too small.Pls enter school next year.");
}else{
System.out.println(s.getName() + "'s age is OK.Pls enter school this year.");
}
}
public static List<Student> getInitStudentList(){
List<Student> list = new ArrayList<>();
list.add(new Student("Alex",getDate("2012-6-6")));
list.add(new Student("xiaoming",getDate("2012-9-6")));
list.add(new Student("jiqiren",getDate("2012-3-16")));
list.add(new Student("xiaohong",getDate("2012-7-2")));
list.add(new Student("fangfang",getDate("2012-10-8")));
list.add(new Student("yagnyang",getDate("2012-1-3")));
return list;
}
private static Date getDate(String dateStr){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try{
date = format.parse(dateStr);
}catch (Exception e){
}
return date;
}
}
class Student{
private String name;
private Date birthday;
public Student(String name,Date birthday){
this.name = name;
this.birthday = birthday;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
打印结果
Alex's age is OK.Pls enter school this year.
xiaoming's age is too small.Pls enter school next year.
jiqiren's age is OK.Pls enter school this year.
xiaohong's age is OK.Pls enter school this year.
fangfang's age is too small.Pls enter school next year.
yagnyang's age is OK.Pls enter school this year.