开发者

Elapsed Time with Joda-Time

I have a DateTime object that I need to se开发者_高级运维e if it's 10 years or more in the past (Think of expiring certifications). I'm new to Joda-Time, how is that done? Thanks for your help.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜