How to convert months into months in Java

My new Java and I want to convert for months to years For example, if I have 18 months and divide it into 12, I will have 1.5 years, but I hope it is 1 year and 6 months

Thanks for your help.

Solution

Use modulus%

int months = 18;
int years = months / 12; // 1
int remainingMonths = months % 12; // 6
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>