• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to round up year duration.

 
Lim Cody
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic