3

我有一个DateTime对象,我需要查看它是否在过去 10 年或更长时间(想想过期的认证)。我是Joda-Time 的新手,那是怎么做的?谢谢你的帮助。

4

2 回答 2

4

您需要查看DateTime 类的文档。但只是为了让你动起来,支票看起来像下面这样:

1)您需要构建一个代表 10 年前的 DateTime ......

// 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) ...并使用 DateTime.isBefore 进行比较。

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

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

请注意,日历中有一些微妙之处,Joda 为您做了很好的抽象;在深入研究之前,您真的很想研究文档。

于 2010-07-07T19:37:56.273 回答
0

我没有和joda一起工作过。
看看是否有Years.yearsBetween帮助(下面的链接)。

它可能看起来像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)

于 2010-07-07T19:41:03.790 回答