如何在java中将月份转换成几个月 [英] How to convert months to years-and-months in java

查看:93
本文介绍了如何在java中将月份转换成几个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于java而言,我想要转换几个月到几年。例如,如果我有18个月,并把它分成12个,我会有1.5年,但我想要1年和6个月。



感谢您的帮助。 p>

解决方案

使用模数

  int months = 18; 
int years = months / 12; // 1
int remainingMonths = months%12; // 6


Im new to java and I want to convert months to years. For example, If i had 18 months and divided that by 12, I would have 1.5 years but I want it as 1year and 6months.

Thanks for your help.

解决方案

Use the modulus %.

int months = 18;
int years = months / 12; // 1
int remainingMonths = months % 12; // 6

这篇关于如何在java中将月份转换成几个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆