Reputation:
I have to find out the age from date of birth placed in a table in a tester class. This means that I have to get the date of the present day of now to the date of a certain date chosen. Moreover, I put the method in a main class. The error is that I keep getting 2018 for all the variables plugged in. From my code (not all in here) it should be 58 for the first one not 2018.
Here is the code.
public static int ageMeth(String dob) {
dob.split("/");
int years = 0;
String tempDate = "";
boolean month = true;
if (month) {
// gets the month converts
int x = Integer.parseInt(dob.valueOf(0));
int y = Integer.parseInt(dob.valueOf(1));
String xy = Integer.toString(x, y);
int xyNum = Integer.parseInt(xy);
LocalDateTime date = LocalDateTime.now();
tempDate = date.toString();
int p = Integer.parseInt(tempDate.valueOf(5));
int i = Integer.parseInt(tempDate.valueOf(6));
String pi = Integer.toString(p, i);
int piNum = Integer.parseInt(pi);
// the days
int x2 = Integer.parseInt(dob.valueOf(2));
int y2 = Integer.parseInt(dob.valueOf(3));
String xy2 = Integer.toString(x2, y2);
int xyNum2 = Integer.parseInt(xy2);
int p2 = Integer.parseInt(tempDate.valueOf(8));
int i2 = Integer.parseInt(tempDate.valueOf(9));
String pi2 = Integer.toString(p2, i2);
int piNum2 = Integer.parseInt(pi2);
// the year
int x3 = Integer.parseInt(dob.valueOf(4));
int y3 = Integer.parseInt(dob.valueOf(5));
int xx3 = Integer.parseInt(dob.valueOf(6));
int yy3 = Integer.parseInt(dob.valueOf(7));
String xy3 = Integer.toString(x3, y3);
String xxyy3 = Integer.toString(xx3, yy3);
int xyNum3 = Integer.parseInt(xy2);
int xxyyNum3 = Integer.parseInt(xxyy3);
int p3 = Integer.parseInt(tempDate.valueOf(8));
int i3 = Integer.parseInt(tempDate.valueOf(9));
String pi3 = Integer.toString(p3, i3);
int piNum3 = Integer.parseInt(pi3);
// the main variables
int newMonth = piNum - xyNum;
int newDay = piNum2 - xyNum2;
int newYear = piNum3 - (xyNum3 + xxyyNum3);
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(newYear, newMonth, newDay);
Period per = Period.between(birthday, today);
years = per.getYears();
}
return years;
}
Upvotes: 1
Views: 86
Reputation: 86286
It seems you’re under a couple of false impressions?
Integer.parseInt(someString.valueOf(someIndex))
will give you the int
value of the digit at index someIndex
in someString
? Not so. Turn on the warnings you can in your IDE (or compiler). It will tell you something like “The static method valueOf(int) from the type String should be accessed in a static way”. This means that valueOf
is not a method of the string in question but a method of the String
class (tihs is what “static” means here). So the result you get from it will not have anything to do with the string.Integer.toString(p, i)
will give you a string formed concatenating p
and i
? Not so either. The documentation says:
Returns a string representation of the first argument in the radix specified by the second argument.
It seems that it will help you to read up on how the methods in the String
class work.
Furthermore you are trying to build a new LocalDate
from the differences of the month, day and year. Instead you should build one from the month, day and year in the string only, and then afterward find the elapsed period between this date and today (like you are already doing).
The correct solution to your problem is in the answer by Nicholas K.
Links
Upvotes: 0
Reputation: 15423
To find the age based on the dob
passed in, you could just use :
public static int ageMeth(String dob) {
return Period.between(LocalDate.parse(dob, DateTimeFormatter.ofPattern("MM/dd/yyyy")),
LocalDate.now()).getYears();
}
Upvotes: 2