0

Could someone tell me what I am doing wrong for getting this StackOverFlowError with Joda library?

Here the code implyed:

  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;
}

Here where I call this method :

@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();
}

I am using the getter method for Hibernate.

How to avoid this error?

The stack trace :

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)

EDIT Joda dependencies and 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>

The method is:

public String getBirthDate() {
    if ( getBirthDay() != null && getBirthMonth() != null && getBirthYear() != null ) {
        birthDate= getBirthDay()+ "/" +  getBirthMonth() + "/" + getBirthYear();
    }
    return birthDate;
}
akuma8
  • 4,160
  • 5
  • 46
  • 82
  • wasn't joda time implemented in java 1.7? – XtremeBaumer Apr 11 '17 at 12:52
  • What does `getBirthDate()` yield as string? @XtremeBaumer No Joda-Time is not part of Java, but in Java-8 a new time library `java.time`-package was introduced mainly by the same author (of Joda-Time) inspired by Joda-Time. However, both libs are very different. – Meno Hochschild Apr 11 '17 at 12:53
  • @Meno Hochschild yes getBirthDate() is a string wich contains "dd/mm/yyyy" – akuma8 Apr 11 '17 at 12:58
  • Are you sure, the pattern itself and not the textual date??? – Meno Hochschild Apr 11 '17 at 12:59
  • 1
    Share the code of `getBirthDate()` method –  Apr 11 '17 at 13:02
  • 1
    @akuma8 You need to provide a [mcve]. – assylias Apr 11 '17 at 13:03
  • 1
    One thing is clear: If `getBirthDate()` really contains the string "dd/mm/yyyy" then we get this exception: `java.lang.IllegalArgumentException: Invalid format: "dd/mm/yyyy"` but not a `StackOverflowError`. – Meno Hochschild Apr 11 '17 at 13:08
  • I added the `getBirthDate()` method. The field concerned are simple string. – akuma8 Apr 11 '17 at 13:09
  • Hm, my suspicion is: The `StackOverflowError` might not be directly related to Joda-Time but was just due to the fact that some internal stack depth in the JVM was exhausted. This can happen if an app has extremely many layers of invocation. – Meno Hochschild Apr 11 '17 at 13:11
  • I'll dig the problem I'll let you know. – akuma8 Apr 11 '17 at 13:14
  • 1
    It's not the question's topic, but to calculate a difference in years you can use `org.joda.time.Years` class and call `Years.yearsBetween(birth, new LocalDate()).getYears()` to get the number of years –  Apr 11 '17 at 13:19
  • @Hugo I will think abut your suggestion next time. I don't know methods offered by this library. – akuma8 Apr 11 '17 at 13:27

2 Answers2

0

I found the reason of this exception, I just forgot to load the required data from database.

akuma8
  • 4,160
  • 5
  • 46
  • 82
-1

Try this:

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

This assumes that getBirthDate() returns the date in the form anticipated in your DateTimeFormat.forPattern() of "dd/MM/yyyy" for example "11/04/2017"

If you must use DateTimeFormatter try this:

 DateTime birth = new DateTime(DateTimeFormat.forPattern("dd/MM/yyyy").parseDateTime(birthdate));
dspano
  • 1,540
  • 13
  • 25
  • 3
    Have you tested your proposal ? You don't even know what `getBirthDate()` yields. And the question is about Joda-Time, not `SimpleDateFormat`. – Meno Hochschild Apr 11 '17 at 13:06