Months between dates

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Months between dates

joaobsneto
Hi,
I want to get days, weeks, months and years between two dates(moments). The API offers this functionality, but it's not working as I expected. My first try was:

return Days.daysBetween(lastUpdate,currentUpdate).getDays();

I think it returns full days between two moments. If a have 2012/05/27 8:00PM as "lastUpdate" and 2012/05/28 2:00AM as "currentUpdate" the function returns 0. I did a little "trick" that worked for days and weeks:

DateTime initialMomentOfDay = new DateTime(lastUpdate.getYear(), lastUpdate.getMonthOfYear(), lastUpdate.getDayOfMonth(), 0, 0);
return Days.daysBetween(initialMomentOfDay,currentUpdate).getDays();

But when it comes to Months it doesn't work even with the "trick". The code I used was:

DateTime initialMomentOfMonth = new DateTime(lastUpdate.getYear(), lastUpdate.getMonthOfYear(), 1, 0, 0);
return Months.monthsBetween(initialMomentOfMonth, currentUpdate).getMonths();

When lastUpdate = new DateTime(2000, 1, 31, 23, 59)
And currentUpdate = new DateTime(2000, 2, 1, 0, 0)
It returns 0 and should return 1.

Cheers,
Joao Neto
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Months between dates

jodastephen
The months calculation returns the number of complete months between
two dates. So, 20th Feb to 19th Apr would be 1 month, but to 20th Apr
would be 2 months.

You can use:
 new Period(date1, date2, PeriodType.yearMonthDay())
to get the whole period in days, months and years, or try this method
to get closer to your needs:
 Period.fieldDifference(date1, date2)

If you only care about dates (and not times) try using LocalDate
rather than DateTime.

Stephen


On 28 May 2012 20:47, joaobsneto <[hidden email]> wrote:

> Hi,
> I want to get days, weeks, months and years between two dates(moments). The
> API offers this functionality, but it's not working as I expected. My first
> try was:
>
> return Days.daysBetween(lastUpdate,currentUpdate).getDays();
>
> I think it returns full days between two moments. If a have 2012/05/27
> 8:00PM as "lastUpdate" and 2012/05/28 2:00AM as "currentUpdate" the function
> returns 0. I did a little "trick" that worked for days and weeks:
>
> DateTime initialMomentOfDay = new DateTime(lastUpdate.getYear(),
> lastUpdate.getMonthOfYear(), lastUpdate.getDayOfMonth(), 0, 0);
> return Days.daysBetween(initialMomentOfDay,currentUpdate).getDays();
>
> But when it comes to Months it doesn't work even with the "trick". The code
> I used was:
>
> DateTime initialMomentOfMonth = new DateTime(lastUpdate.getYear(),
> lastUpdate.getMonthOfYear(), 1, 0, 0);
> return Months.monthsBetween(initialMomentOfMonth,
> currentUpdate).getMonths();
>
> When lastUpdate = new DateTime(2000, 1, 31, 23, 59)
> And currentUpdate = new DateTime(2000, 2, 1, 0, 0)
> It returns 0 and should return 1.
>
> Cheers,
> Joao Neto
>
> --
> View this message in context: http://joda-interest.219941.n2.nabble.com/Months-between-dates-tp7572425.html
> Sent from the Joda-Interest mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...