Title: Joda Time - Get date difference
Tags: java;jodatime
Question: I'm currently trying to get the difference between two date and return the difference in months. Somehow i can't get it right (read other threads about JT). Here's what i have so far:

```public final class DateUtils {

    private static DateTime dateTime = new DateTime();
    private  static Integer months;

    /**
     * Gets the difference in months, between local time and user's birth date
     */

    public static Integer getMonthDiff(User user) {

        months = Months.monthsBetween(dateTime, user.getUserDetails().getBirthDate()).getMonths();
        return months;
    }
}
```

I'm using this Class / Method throughout my code, in order to get for example, users with the account created last year (just an example).

```public List<User&gt; findCreatedLastYear() {

    List<User&gt; users = findAll();
    List<User&gt; tempList = new ArrayList<&gt;();

    for (User user : users) {

        if( DateUtils.getMonthDiff(user) == 12){
            tempList.add(user);
        }
    }

    LOGGER.log(Level.INFO, String.format("{%d} user(s)created an account last year", tempList.size()));
    return tempList;
}
```

My JUnit test fails and i'm not sure where the mistake is. 

```@Test
public void testFindLastYear() throws Exception {
    User user = userService.findAll().get(0);

    user.setAccountCreated(new DateTime(2014, 3, 3, 0 , 0, 0));
    Assert.assertNotNull(userService.findCreatedLastYear());
}
```

It fails as it found 0 users with that condition. However i'm not using a DB (i'm generating the users through a class).

The cause I think is that somewhere i'm not using Joda Time correctly.

Here is what i get from UT: INFO: {0} user(s)created an account last year

Here is how i generate (simulate a DB).

List users = new ArrayList<>();

```for (int i = 0; i < size; i++) {
    User user = new User();

    user.setUsername("username_" + i);
    user.setPassword("psd_" + i);
    user.setEmail("username_" + i + "@dnet.org");
    user.setAccountCreated(new DateTime());
    user.setLastLogin(new DateTime());


    UserDetails userDetails = new UserDetails();

    userDetails.setFirstname("FirstName_" + i);
    userDetails.setLastname("LastName_" + i);
    userDetails.setBirthDate(new DateTime());

    user.setUserDetails(userDetails);

    users.add(user);
}
```

Comment: Wouldn't it be easier to retrieve data using SQL (assuming you're using DB connection)? SELECT e FROM User e WHERE e.dateCreated BETWEEN :before AND :after

Comment: How does your test fail? You do get an empty list even if no matches are found. Also, why are you only testing for `== 12`? Do you only want to find from the same month last year?

Comment: I'm testing for 12 as my method should return the diff in months. So I want to get all users from the list where the diff between their birthdate and current system time is 12.

Comment: It doesn't have to be like this exactly, interested in othere approaches as well for this.



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:07/20/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><jodatime>', 'Title': 'Joda Time - Get date difference', 'ViewCount': 72, 'id': 31518254, 'user_AboutMe': None, 'user_AccountId': 2210901.0, 'user_DisplayName': 'Alex L', 'user_Reputation': 153.0, 'user_Views': 4.0, 'user_id': 1952786.0}}