开发者

How do I store a date and find how many days have passed since that date?

I am creating an Android app that will calculate the interest on a loan. How do I store a date that is selected from a DatePicker, then calculate how many days have passed between the current 开发者_运维知识库date and the stored date?


There's java.util.GregorianCalendar in the standard library, which you can initialise with the data from the DatePicker.

Alternatively, you could use JodaTime, which I'm informed provides a nicer library for dates and times (I've not actually given it a go myself, but it's definitely worth looking at).

http://joda-time.sourceforge.net/

As for calculating the number of days between two dates, JodaTime has the Days.daysBetween method, which makes it pretty simple. To use that it's probably simplest to store the two dates and times in LocalDate objects (though this assumes that you've not got any time zone issues to think about).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜