質問

This Dateclass handles the age of each members and calculate the persons age by getting his birthday. Then we get actual date and substract it with birthday.

My problem is that a can't substract a SimpleDateFormat and a String. How can I fix this problem?

Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyy");
System.out.print(sdf.format(dt));

ArrayList<String> Mem = Data.getCpr();
String newStr = Mem.toString();

This is a birthday we wish to use to substract

String birth = newStr.substring(1, 7);
System.out.print(" "+birth);

This is the substraction of a SimpleDateFormat and a String

int age = sdf - birth;  
役に立ちましたか?

解決

try using the Calendar class, instantiate one using the current time, and the other using the members birthday, then use the add method to figure out the age. Remember that you will have to compare the month and day as well otherwise you will end up just with the age this year rather than the members real age.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top