有人能告诉我用 Joda 库获取这个 StackOverFlowError 做错了什么吗?
这里的代码暗示:
public Integer getAge() {
if ( getBirthDate() != "//" ) {
try {
LocalDate birth = LocalDate.parse( getBirthDate(), DateTimeFormat.forPattern( "dd/MM/yyyy" ) );//Error raised here
DateTime today = new DateTime();
if ( today.getMonthOfYear() >= birth.getMonthOfYear() ) {
age = today.getYear() - birth.getYear();
} else {
age = today.getYear() - birth.getYear() - 1;
}
} catch ( Exception e ) {
e.printStackTrace();
}
}
return age;
}
我在这里调用这个方法:
@Override
public boolean equals( Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( !( obj instanceof Identite ) ) {
return false;
}
Identity other = (Identity) obj;
EqualsBuilder equalsBuilder = new EqualsBuilder();
equalsBuilder.append( getAge(), other.getAge() );//here the call
return equalsBuilder.isEquals();
}
我正在为 Hibernate 使用 getter 方法。
如何避免这个错误?
堆栈跟踪:
java.lang.StackOverflowError
at org.joda.time.chrono.BasicChronology.getYear(BasicChronology.java:426)
at org.joda.time.chrono.BasicGJChronology.setYear(BasicGJChronology.java:180)
at org.joda.time.chrono.BasicYearDateTimeField.setExtended(BasicYearDateTimeField.java:92)
at org.joda.time.format.DateTimeParserBucket$SavedField.set(DateTimeParserBucket.java:568)
at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:447)
at org.joda.time.format.DateTimeParserBucket.computeMillis(DateTimeParserBucket.java:411)
at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:887)
at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:844)
at org.joda.time.LocalDate.parse(LocalDate.java:179)
at com.home.entities.Identity.getAge(Identite.java:127)
at com.home.entities.Identity.equals(Identite.java:185)
编辑Joda 依赖项和 getBirthDate():
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-convert</artifactId>
<version>1.8.1</version>
</dependency>
方法是:
public String getBirthDate() {
if ( getBirthDay() != null && getBirthMonth() != null && getBirthYear() != null ) {
birthDate= getBirthDay()+ "/" + getBirthMonth() + "/" + getBirthYear();
}
return birthDate;
}