package com.kfm.day0907.ex;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
/**
*
* 编写一个生日提醒程序,使用Map来存储人名和对应的生日日期。够根据日期提醒用户哪些人今天过生日?
*
* - 如何筛选显示出最近七天过生日的用户
* @author by FZB
* @date 2023/9/7
*/
public class Ex06 {
public static void main(String[] args) {
Map<String, LocalDate> map = new HashMap<>();
map.put("张三",LocalDate.of(2022,1,02));
map.put("王五",LocalDate.of(2001,2,02));
map.put("黄飞鸿",LocalDate.of(2012,6,02));
map.put("李四",LocalDate.of(2012,9,06));
map.put("小李",LocalDate.of(1992,1,02));
map.put("奥特曼",LocalDate.of(1982,5,07));
map.put("蔡徐坤",LocalDate.of(1872,1,02));
map.put("霍元甲",LocalDate.of(1562,9,07));
map.put("叶问",LocalDate.of(1222,8,31));
map.put("孙悟空",LocalDate.of(1350,1,02));
map.put("猪八戒",LocalDate.of(2012,9,07));
map.put("沙僧",LocalDate.of(2012,9,14));
//当前日期前七天的日期
LocalDate preLocalDate = LocalDate.now().minusDays(7);
//当前日期后十四天的日期
LocalDate lastLocalDate = LocalDate.now().minusDays(-7);
//提醒用户今天生日
map.entrySet().stream().forEach(e -> {
Boolean isBirthday = e.getValue().toString().substring(5,10).equals(LocalDate.now().toString().substring(5,10));
if(isBirthday){
System.out.println("尊敬的\t"+e.getKey()+"\t先生/女士,祝您生日快乐!生活愉快!" );
}
if(!isBirthday && parseInteger(e.getValue()) >= parseInteger(preLocalDate) && parseInteger(e.getValue()) <= parseInteger(lastLocalDate)){
if(parseInteger(e.getValue()) < parseInteger(LocalDate.now())){
System.out.println("尊敬的\t"+e.getKey()+"\t先生/女士,迟来的生日祝福请您收下!" );
}else{
System.out.println("尊敬的\t"+e.getKey()+"\t先生/女士,您的生日快到了,提前祝您生日快乐!" );
}
}
});
}
//生日转整数
public static Integer parseInteger(LocalDate localDate){
return Integer.valueOf(localDate.toString().substring(5, 10).replaceAll("-", ""));
}
}
11-30
5129
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-29
07-12