LoginSignup
1
0

More than 3 years have passed since last update.

○ヶ月から、○年○ヶ月に変換する

Last updated at Posted at 2020-06-04

概要

月数から、年数と残りの月数を求める方法です。(Javaで書いてます)

35ヶ月は2年11ヶ月です

このような感じ。

変換方法

コードはこんな感じ。

YearMonth.java
public class YearMonth{
    public static void main(String args[]){
        int monthCount = 35;
        System.out.println(monthCount + "ヶ月は" + monthCount/12 + "年" + monthCount%12 + "ヶ月です");
    }
}

変数monthCountに格納された35ヶ月が、年月に変換されています。

年とヶ月は別々で計算しました。

年の方は、35ヶ月を12ヶ月(1年)で割り、Math.floorで小数点を切り捨て。
そしてそのままだと小数点が.0と付いてしまったので、前に(int)を置いて整数に。

ヶ月の方は、35ヶ月から12ヶ月(1年)を割った余りにすることで計算できました。

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0