org.joda.time.chrono.BasicChronology.getYear(BasicChronology.java:426)

时间:2017-04-11 12:48:27

标签: java stack-overflow jodatime joda-convert

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

试试这个:

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

这假设getBirthDate()返回DateTimeFormat.forPattern()中预期形式的日期&#34; dd / MM / yyyy&#34;例如&#34; 11/04/2017&#34;

如果必须使用DateTimeFormatter,请尝试:

 DateTime birth = new DateTime(DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(birthdate));