Easy way to convert # of months into # of years and months in Groovy
Is there an easy way in G开发者_Python百科roovy to convert months (ex: 58 months) into years and months.. 4 years, 10 months?
Thanks!
Basic concept, not really tied to any language:
58 / 12 = 4
58 % 12 = 10
Unless you are asking some sort of trick question? :-)
Here's a simple solution:
def months = 58
println "${(months / 12) as int} years, ${months % 12} months"
It doesn't handle the edge cases like using singular for one year or one month, or omitting the years/months part in case they are zero.
As an alternative you could use a Java library like PrettyTime, too.
精彩评论