我正在尝试将生日从字符串转换为日期,以便添加到数组列表联系人中。然后从联系人中读取以显示生日。但是 05/02/1990 变为 365/12/1990,06/12/1991 变为 365/12/1991(即 DD 和 MM 不正确)。非常感谢你的帮助!
ArrayList<Person> contacts = new ArrayList<Person>();
...
String firstName = sc.next();
String lastName = sc.next();
String email = sc.next();
String birthdayStr = sc.next();
SimpleDateFormat formatter = new SimpleDateFormat("DD/MM/YYYY");
try{
Date birthday = formatter.parse(birthdayStr);
Person s = new Person(firstName, lastName, email, birthday);
contacts.add(s);
Date b = s.getBirthday();
System.out.println(formatter.format(b));
}catch(ParseException e){
System.out.println( e.getMessage());
}