0

有人能告诉我用 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;
}
4

2 回答 2

0

我找到了这个异常的原因,我只是忘记从数据库中加载所需的数据。

于 2017-04-19T11:05:59.163 回答
-1

试试这个:

DateTime birth = new DateTime(new SimpleDateFormat("dd/MM/yyyy").parse(getBirthDate()));

这假设 getBirthDate() 以“dd/MM/yyyy”的 DateTimeFormat.forPattern() 中预期的形式返回日期,例如“11/04/2017”

如果你必须使用 DateTimeFormatter 试试这个:

 DateTime birth = new DateTime(DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(birthdate));
于 2017-04-11T13:04:51.220 回答