tags:

views:

39

answers:

2

Hello, I have a DataTime field that I need to see if it's 10 years or more in the past (Think of expiring certifications). I'm new to Joda, how is that done? Thanks for your help.

+1  A: 

You'll want to review the docs for the DateTime class. But just to get you moving, the check would look something like the following:

1) You'll need to construct a DateTime which represents 10 years ago...

// Build a DateTime for exactly 10 years ago.

DateTime tenYearsAgo = new DateTime(); // should give you a DateTime representing 'now' 
tenYearsAgo = tenYearsAgo.minusYears(10);            // should give you 10 years ago

2) ...and use DateTime.isBefore to compare.

// Let's assume the DateTime you're comparing is called 'myDateTime'.

if (myDateTime.isBefore(tenYearsAgo)) { /* do something */ }
else { /* do something else */ }

Note there are some subtleties with calendars which Joda does a nice job of abstracting for you; before you get too deep in this, you'll really want to study the docs.

Joe
A: 

I have not worked with joda.
See if Years.yearsBetween helps (link below).

It might look like Years.yearsBetween(myDateTime, new DateTime());

[1] http://joda-time.sourceforge.net/api-release/org/joda/time/Years.html#yearsBetween(org.joda.time.ReadableInstant,%20org.joda.time.ReadableInstant)

shahkalpesh