开发者

Easy way to convert # of months into # of years and months in Groovy

开发者 https://www.devze.com 2023-01-25 23:55 出处:网络
Is there an easy way in G开发者_Python百科roovy to convert months (ex: 58 months) into years and months.. 4 years, 10 months?

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.

0

精彩评论

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