i'm trying to compare a personalnumber with the current date to get the persons age.
The current problem is that if my personalnumber has a zero in it, it gets removed so i can't parse it.
for (int i = 0; i <= personList.size(); i++) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date = formatter.format(new Date());
String temp = personList.get(i).getCPR();
int year = Integer.parseInt((temp.substring(4, 6)));
if (year < 20) {
year += 2000;
} else {
year += 1900;
}
int month = Integer.parseInt(temp.substring(2, 4));
int day = Integer.parseInt(temp.substring(0, 2));
/*if (month if month don't start with zero){
add 0 to month on the left
}
same goes for day*/
String birthday = year + "-" + month + "-" + day;
LocalDate date1 = LocalDate.parse(birthday);
LocalDate date2 = LocalDate.parse(date);
long age = date1.until(date2, ChronoUnit.YEARS);
System.out.println(age);
}
this is the error i'm getting
Exception in thread "main" java.time.format.DateTimeParseException: Text '1995-1-13' could not be parsed at index 5
I want 1995-1-13 to read 1995-01-13
Aucun commentaire:
Enregistrer un commentaire