这是家庭作业。
目标:我想比较两个对象的日期以确定我的人对象是否是成年人,并将其存储在一个字符串中。
奇怪的是,我所有的日期 d1 值都是 0;
public class Date {
public int day, month, year;
public String child
Date(date d1, date d2) {
if ((d1.year - d2.year > 18) ||
((d1.year - d2.year == 18) && (d2.year> d1.year)) ||
((d1.year - d2.year == 18) && (d2.year == d1.maand) && (d2.day > d1.day))) {
child = adult;
} else {
child = child;
}
Date(int a, int b, int c) {
a = year;
b = month;
c = day;
}
Date (String birthdate) {
String pattern = "\\d{2}-\\d{2}-\\d{4}";
boolean b = birthdate.matches(pattern);
if (b) {
String[] str = birthdate.split("-");
for (String s: str)
this.day = Integer.parseInt(str[0]);
this.month = Integer.parseInt(str[1]);
this.year = Integer.parseInt(str[2]);
this.child = false;
} else {
System.out.println("Wrong format");
}
}
当我进行测试时,会发生这种情况:
System.out.println("D1 year = " + d1.year); System.out.println("D1 day = " + d1.day); System.out.println("D1 month = " + d1.month);
Result:
D1 year = 0
D1 day = 0
D1 month = 0
为什么会这样?让我们看看我的另一堂课。
我的另一个类,我的方法 infoPerson 所在的位置如下:
public static Person infoPerson() {
String name, lastname, birthdate;
Datum birthday, today;
System.out.println("Firstname:");
name = userInput();
System.out.println("Lastname:");
lastname = userInput();
System.out.println("Birthdate?:");
birthdate = userInput();
//here I send the string birthdate to my Date class
birthday = new Date(birthdate);
today = new Date(3, 7, 2013);
//Here I want to compare my two Date objects, today and birthday. This is were I got stuck, how do I do this correctly?
dateChild = new Date(today, birthday);
// here i send the new date to my Person class what consists of two strings and Data birthday
return new Gast(name, lastname, dateChild);
}