帮我用java写一个可以自动提醒生日的程序
时间: 2023-04-09 17:04:42 浏览: 67
好的,以下是一个可以自动提醒生日的Java程序:
```java
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.List;
public class BirthdayReminder {
private List<Person> persons;
public BirthdayReminder() {
persons = new ArrayList<>();
}
public void addPerson(Person person) {
persons.add(person);
}
public void remind() {
LocalDate today = LocalDate.now();
for (Person person : persons) {
LocalDate birthday = person.getBirthday();
Period period = Period.between(today, birthday);
int days = period.getDays();
if (days == 0) {
System.out.println("今天是 " + person.getName() + " 的生日!");
} else if (days == 1) {
System.out.println("明天是 " + person.getName() + " 的生日!");
} else if (days <= 7) {
System.out.println(person.getName() + " 的生日还有 " + days + " 天!");
}
}
}
public static void main(String[] args) {
BirthdayReminder reminder = new BirthdayReminder();
reminder.addPerson(new Person("张三", LocalDate.of(1990, 5, 20)));
reminder.addPerson(new Person("李四", LocalDate.of(1995, 6, 15)));
reminder.addPerson(new Person("王五", LocalDate.of(2000, 7, 10)));
reminder.remind();
}
}
class Person {
private String name;
private LocalDate birthday;
public Person(String name, LocalDate birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public LocalDate getBirthday() {
return birthday;
}
}
```
你可以在程序中添加你想要提醒的人的姓名和生日,然后运行程序即可。