Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# How to round up year duration.

Lim Cody
Greenhorn
Posts: 6
Let say start date is 25/5/2000 and end date is 25/6/2000. The duration in month is 1 month only then i need return 1 year. Example: if the actual duration is 2 years 3 month the method return 3 years. Can someone help me? Thanks.

Ranch Hand
Posts: 134
So if hte duration is less than 365 days, you want your app to display one year. If it is less than 3*365 days, you want it to display 3? I wonder what would happen if your application divided 30 days by some constant equal to the number of days in a year?

Lim Cody
Greenhorn
Posts: 6
yes, Chad Clites's solution is good. but i don't know how to get number of days in a year (365 or 366)? example 25/5/2000 to 24/5/2001 should return 1 year (365 days) and 25/5/2003 to 24/5/2004 (leap year) also return 1 year (366 day).

Remko Strating
Ranch Hand
Posts: 893
Use the java.util.Calendar class for computing the year of both days.

Subtract the values of the years and add one to it.

Java.Util.Calendar